









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The document includes answers and codes to some object oriented programming questions
Typology: Exercises
1 / 15
This page cannot be seen from the preview
Don't miss anything!










#include
class department { protected: string dname; string dhead; int did; public: department() { } department(string^ a, string^ b, int^ c) { dname = a; dhead = b; did = c; } void setdname(string a) { dname = a; } void setdhead(string a) { dhead = a; } void setdid(int a) { did = a; } string getdname() { return dname; } string getdhead() { return dhead; } int getdid() { return did; } void input() { cout << "Department Name: "; cin >>^ dname; cout << "Department Head Name: "; cin >>^ dhead; cout << "Department ID: "; cin >>^ did; } void display() { cout <<^ endl; cout << "Department Name: "^ <<^ dname^ << endl; cout << "Department Head: "^ <<^ dhead^ << endl; cout << "Department ID: "^ << did^ << endl; } };
objectclass() {} objectclass(string^ a, string^ b, int^ c, string^ d, string^ e, int^ f,^ int^ g, string h, float i) :company(a, b, c), department(d, e, f), employee(g, h, i) {} void display() { company::display(); department::display(); employee::display(); } void input() { company::input(); department::input(); employee::input(); } };
int main() { objectclass obj("WAPDA", "Faisalabad", 041260231, "FESCO", "Ghori", 12, 012, "Ashas", 24522.12); objectclass obj1[3]; for (int i = 0; i < 3; i++) { cout << "For" <<^ i+2 << " Object, "^ << endl; obj1[i].input(); cout <<^ endl; } cout <<^ endl; cout << "Press Enter to Continue."; getchar(); system("cls"); cout << "Object#1"^ << endl; obj.display(); cout <<^ endl; for (int i = 0; i < 3; i++) { cout << "Object#" <<^ i + 2^ << endl; obj1[i].display(); cout <<^ endl; } cout <<^ endl; system("pause"); return 0;
#include
class Rectangle : public Shape { public: double width; double height;
cout << endl; } };
int main() { Rectangle r; elipse e; Triangle t; Shape *ptr; ptr = &r; ptr->input(); ptr->display(); cout << "Area: "^ << ptr->area() << endl^ << endl; ptr = &e; ptr->input(); ptr->display(); cout << "Area: "^ << ptr->area() << endl^ << endl; ptr = &t; ptr->input(); ptr->display(); cout << "Area: "^ << ptr->area() << endl^ << endl; cout <<^ endl; system("pause"); return 0; }
#include
class Rectangle : public Shape { public: double width; double height; Rectangle() {} double area() { return height*width; }
void input() { Shape::input(); } void display() { Shape::input(); cout <<^ endl; } }; class elipse :public Shape { public: double major_axis; double minor_axis; elipse() {} double area() { return major_axis*minor_axis; } void input() { Shape::input(); }
#include
cout << "Name of Dog: " << nimi; } }; class Cat :public Mammal { public: void printinformation() { cout << "Name of Cat: " <<^ nimi; } }; int main() { Mammal *ptr; cout << "Which class do you want to make?"^ << endl << "1) Dog" << endl << "2) Human" << endl << "3) Cat" << endl << "Your choice: "; int a; cin >>^ a; switch (a) { case 1: ptr = new Dog(); break; case 2: ptr = new Human(); break; case 3: ptr = new Cat(); break; default: cout << "Invalid input"; system("pause"); return 0; break; } cout << "Enter Name: "; string b; getchar(); getline(cin, b); ptr->setname(b); cout <<^ endl; ptr->printinformation(); cout <<^ endl; system("pause"); return 0; }
class stringed : public instrument { public: void play() {} char* what() { char a; return &a; } void adjust() {} }; class woodwind :public wind { public: void play(){} char* what(){ char a; return &a; } }; class brass :public wind { public: void play() {} char* what() { char a; return &a; } };
int main()
{ instrument *p; int a; cout << "What instrument you would like to create?"^ << endl; cout << "1) Wind" <<^ endl << "2) Precussion" << endl << "3) Stringed" << endl << "Your choice: " << endl; cin >>^ a; switch (a) { case 1: cout << "Which sub derive class you want to create?"^ << endl << "1) Woodwind" << endl << "2) Brass" << endl << "Your choice: "; cin >>^ a; switch (a) { case 1: p = new^ woodwind(); break; case 2: p = new^ brass(); break; default: cout << "Invalid Choice"^ <<^ endl; system("pause"); return 0; }
break; case 2: p = new^ percussion(); break; case 3: p = new^ stringed(); break; default: cout << "Invalid Choice"^ <<^ endl; system("pause"); return 0; } cout <<^ endl; system("pause"); return 0; }
#include