




























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























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
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
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
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
Task 4.2 Write a C++ program to overload arithmetic assignment operator “=” #include
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
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
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