Docsity

OOPs Lab Manual With Complete Solution in C++ and Python, Lab Reports of Object Oriented Programming

Toggle sidebar
bg1
OOPs Lab 1
Task 1.1: Create a class called Invoice that a hardware store might use to represent an
invoice for an item sold at the store. An Invoice should include four data members—a part number
(type string), a part description (type string), a quantity of the item being purchased (type int) and
a price per item (type int). Provide a set and a get function for each data member. In addition,
provide a member function named getInvoiceAmount that calculates the invoice amount (i.e.,
multiplies the quantity by the price per item), then returns the amount as an int value. Write a test
program that demonstrates class Invoice’s capabilities.
#include<iostream>
#include <string>
using namespace std;
class Invoice
{
public:
Invoice( string, string, int, int );
void setPartNumber( string );
string getPartNumber();
void setPartDescription(string);
string getPartDescription();
void setItemQuantity(int);
int getItemQuantity();
void setItemPrice(int);
int getItemPrice();
int getInvoiceAmount();
private:
string partNumber;
string partDescription;
int itemQuantity;
int itemPrice;
};
Invoice::Invoice( string number, string description, int quantity, int price )
{
partNumber=number;
partDescription=description;
if(quantity>0)
itemQuantity=quantity;
else
{
itemQuantity=0;
cout<<"Initial quantity was invalid."<<endl;
}
if(price>0)
itemPrice=price;
else
{
itemPrice=0;
cout<<"Initial price was invalid."<<endl;}
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download OOPs Lab Manual With Complete Solution in C++ and Python and more Lab Reports Object Oriented Programming in PDF only on Docsity!

Task 1.1: Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members—a part number (type string), a part description (type string), a quantity of the item being purchased (type int) and a price per item (type int). Provide a set and a get function for each data member. In addition, provide a member function named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as an int value. Write a test program that demonstrates class Invoice’s capabilities. #include #include using namespace std; class Invoice { public: Invoice( string, string, int, int ); void setPartNumber( string ); string getPartNumber(); void setPartDescription(string); string getPartDescription(); void setItemQuantity(int); int getItemQuantity(); void setItemPrice(int); int getItemPrice(); int getInvoiceAmount(); private: string partNumber; string partDescription; int itemQuantity; int itemPrice; }; Invoice::Invoice( string number, string description, int quantity, int price ) { partNumber=number; partDescription=description; if(quantity>0) itemQuantity=quantity; else { itemQuantity=0; cout<<"Initial quantity was invalid."<<endl; } if(price>0) itemPrice=price; else { itemPrice=0; cout<<"Initial price was invalid."<<endl;}

void Invoice::setPartNumber( string number) { if ( number.length() <= 25 ) partNumber = number; if ( number.length() > 25 ) { partNumber = number.substr( 0, 25 ); cout << "Name "" << number <<"" exceeds maximum length (25).\n"<< "Limiting partNumber to first 25 characters.\n" << endl; } } void Invoice::setPartDescription(string description ) { if ( description.length() <= 25 ) partDescription = description; if ( description.length() > 25 ) { partDescription = description.substr( 0, 25 ); cout << "Name "" << description <<"" exceeds maximum length (25).\n"<< "Limiting partDescription to first 25 characters.\n" << endl; } } void Invoice::setItemQuantity(int quantity ) { if(quantity>0) itemQuantity=quantity; else { itemQuantity=0; cout<<"Initial quantity was invalid."<<endl; } } void Invoice::setItemPrice(int price ) { if(price>0) itemPrice=price; else { itemPrice=0; cout<<"Initial price was invalid."<<endl; } } string Invoice::getPartNumber() { return partNumber; }

Task 1.2: Create a class called Employee that includes three pieces of information as data members—a first name (type string), a last name (type string) and a monthly salary (type int). Provide a set and a get function for each data member. Write a test program that demonstrates class Employee’s capabilities. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again. #include #include using namespace std; class Employee { public: Employee( string, string, int ); void setFirstName( string ); string getFirstName(); void setLastName(string); string getLastName(); void setMonthlySalary(int); int getMonthlySalary(); private: string firstName; string lastName; int monthlySalary; }; Employee::Employee( string first, string last, int salary) { firstName=first; lastName=last; if(salary>0) monthlySalary=salary; else { monthlySalary=0; cout<<"Initial salary was invalid."<<endl; } } void Employee::setFirstName( string first) { if ( first.length() <= 25 ) firstName = first; if ( first.length() > 25 ) { firstName = first.substr( 0, 25 ); cout << "Name "" << first <<"" exceeds maximum length (25).\n"<< "Limiting firstName to first 25 characters.\n" << endl;}}

void Employee::setLastName(string last ) { if ( last.length() <= 25 ) lastName = last; if ( last.length() > 25 ) { lastName = last.substr( 0, 25 ); cout << "Name "" << last <<"" exceeds maximum length (25).\n"<< "Limiting lastName to first 25 characters.\n" << endl; } } void Employee::setMonthlySalary(int salary ) { if(salary>0) monthlySalary=salary; else { monthlySalary=0; cout<<"Initial salary was invalid."<<endl; } } string Employee::getFirstName() { return firstName; } string Employee::getLastName() { return lastName; } int Employee::getMonthlySalary() { return monthlySalary; } int main() { system("color f0"); Employee Employee1("Jamal","Khan",2000); cout << "Employee1's name is: "<< Employee1.getFirstName()<<" "<< Employee1.getLastName()<<endl; cout<< "and monthly salary is: "<< Employee1.getMonthlySalary()<< " and annual salary is: "<< Employee1.getMonthlySalary()12<< endl; cout<<"After 10 percent increment in salary:"<<endl; int incrementedSalary1=Employee1.getMonthlySalary(); incrementedSalary1=incrementedSalary1+(incrementedSalary10.1); Employee1.setMonthlySalary(incrementedSalary1); cout<< "Employee1's monthly salary is: "<< Employee1.getMonthlySalary()<< " and annual salary is: "<< Employee1.getMonthlySalary()*12<<endl<<endl;}

void Date::setYear(int y ) {year=y;} int Date::getMonth(){ return month;} int Date::getDay(){ return day;} int Date::getYear() {return year;} void Date::displayDate(){ cout<<month<<"/"<<day<<"/"<<year<<endl;} int main() { system ("color f0"); Date Date1(8,18,2019); cout<<"Date is "; Date1.displayDate(); Date1.setMonth(0); Date1.setYear(2000); Date1.displayDate(); }

Task 2.2: Create a class Area to handle area related functionalities. It has two data members’ length and breadth. Define member functions GetLength(), AreaCalculation() and DisplayArea() to get length from the user, calculate the area and display the area respectively. Define two constructors, one with no argument and the other one with two arguments to initialize those variables. Write a test program that demonstrates class Area’s capabilities. #include #include<stdlib.h> using namespace std; class Area { private: float length,width; public: Area () {length=0; width=0; } Area (float l,float b) { length=l; width=b; } void set_data(float l,float b) { length=l; width=b; } void get_Length() { float lngth,bradth; cout<<"Enter length of Ractengle: "; cin>>lngth; cout<<"Enter width of Ractengle: "; cin>>bradth; set_data(lngth,bradth); } void AreaCalculation() { float area; area=length*width; displayArea(area,length,width); } void displayArea(float area,float length,float width)

void String::change(const char *str) { delete [] s; size = strlen(str); s = new char[size+1]; strcpy(s, str); } String::String(const String& old_str) { size = old_str.size; s = new char[size+1]; strcpy(s, old_str.s); } int main() { String str1("GeeksQuiz"); String str2 = str1; str1.print(); // what is printed? str2.print(); str2.change("GeeksforGeeks"); str1.print(); // what is printed now? str2.print(); return 0; }

Task 3.2: Create a class Car. It has three data members’ color, model and price. Declare a static variable carCount of type int. Define a constructor to initialize these variables. Also use the carCount variable in constructor to count number of cars (objects) created. Define member function display() to show all the properties of a car. Also define a static member function getCount() that returns the number of cars (objects) created. Write a test program that demonstrates class Box’s capabilities. #include #include<stdlib.h> using namespace std; class Car { private: string color,model; int price; public: static int car_Count; Car(string clr,string mdl,int prc) { color=clr; model=mdl; price=prc; car_Count++; } void display() { cout<<"Car color: "<<color<<endl; cout<<"Car model: "<<model<<endl; cout<<"Car price: "<<price<<endl; }}; int Car::car_Count=0; int main() { system("color F0"); Car car1("Blue","carR-13",90000); Car car2("PInk","GM-215",80000); Car car3("REd","TX-20",20000); cout<<"1st car details are: "<<endl; car1.display(); cout<<endl; cout<<"2nd car details are: "<<endl; car2.display(); cout<<endl; cout<<"3rd car details are: "<<endl; car3.display(); cout<<"Number of objects you created are: "<<Car::car_Count; return 0; }

Task 4.2 Write a C++ program to overload arithmetic assignment operator “=” #include using namespace std; class Marks { private: int m1; int m2; public: //Default constructor Marks() { m1 = 0; m2 = 0; } // Parametrised constructor Marks(int i, int j) { m1 = i; m2 = j; } // Overloading of Assignment Operator void operator=(const Marks &M ) { m1 = M.m1; m2 = M.m2; }

void Display() { cout << "Marks in 1st Subject:" << m1<<endl; cout << "Marks in 2nd Subject:" << m2<<endl; } }; int main() {system("color f0"); Marks Mark1(45, 89); cout << " Marks of student : "<<endl; Mark1.Display(); return 0;}

int main() { system("color f0"); Displacement d1(200,"d1"); // Displacement object Displacement d2(30,"d2"); // Displacement object d1>d2; d1<d2; return 0; } Task 5.1 Create a class vehicle that has two data members; ‘color’ and ‘price’. Define a member function getVehicle to get these details. Inherit a class car from the class vehicle that has one data member ‘model’. Define a member function getCar to get value for model. Define a member function show to display all these details. #include #include<stdlib.h> using namespace std; class Vehicle { protected: string color; int price; public: Vehicle() { color=" "; price=0; } void get_vehicle() { cout<<"Enter Car Color: "; cin>>color;

cout<<"Enter Car Price: "; cin>>price; } }; class Car:public Vehicle { protected: string model; public: Car() { model=" "; } void get_car() { cout<<"Enter Model Of The Car: "; cin>>model; } void show() { cout<<"Color :"<<color<<endl; cout<<"Price:"<<price<<endl; cout<<"Model:"<<model<<endl; } }; int main() { system("color F0"); Car car1; cout<<"Enter Car Detail:"<<endl; car1.get_vehicle(); car1.get_car(); cout<<"given car details :"<<endl; car1.show(); cout<<endl; return 0; }

Task 5.2 Code a C++ program for the following diagram #include #include<stdlib.h> using namespace std; class Person { protected: void Talk(){cout<<"The Talk function of parent class Person has been called."<<endl;} void Walk(){cout<<"The Walk function of parent class Person has been called."<<endl;} }; class Teacher:public Person { private: void Teaching(){cout<<"The Teaching function of child class Teacher has been called."<<endl;} public: display_Teacher() { Talk(); Walk(); Teaching(); } }; class Player:public Person { private: void Play(){cout<<"The Play function of child class Player has been called."<<endl;} public: display_Player() { Teacher Talk() Walk() Teaching() Player Talk() Walk() Play() Person Talk() Walk() Designer Talk() Walk() Design()

Talk(); Walk(); Play(); } }; class Designer:public Person { private: void Design(){cout<<"The Design function of child class Designer has been called."<<endl;} public: display_Designer() { Talk(); Walk(); Design(); } }; int main() { system("color F0"); Teacher T; Player P; Designer D; cout<<"Teacher class object :"<<endl; T.display_Teacher(); cout<<endl; cout<<"Player class object :"<<endl; P.display_Player(); cout<<endl; cout<<"Designer class object :"<<endl; D.display_Designer(); cout<<endl; return 0; } Lab Task 6.1: Create a class personalDetails which has three data members name, gender and address. This class has a member function getPersonalDetails to get these details from user. Create a new class deptDetails which has also three data members deptName, Id and assignedTask. This class has a