Object Oriented Programming Questions and Answers, Exercises of Object Oriented Programming

The document includes answers and codes to some object oriented programming questions

Typology: Exercises

2018/2019

Uploaded on 04/28/2019

hassanashas
hassanashas 🇵🇰

1 document

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Question 1
#include <iostream>
#include <string>
using namespace std;
class company {
protected:
string cname;
string caddress;
int phone;
public:
void setcname(string a)
{
cname = a;
}
void setcadd(string a)
{
caddress = a;
}
void setphone(int a)
{
phone = a;
}
string getcname()
{
return cname;
}
string getcadd()
{
return caddress;
}
int getphone()
{
return phone;
}
company()
{}
company(string a, string b, int c)
{
cname = a;
caddress = b;
phone = c;
}
void input()
{
cout << "Company Name: ";
cin >> cname;
cout << "Company Address: ";
cin >> caddress;
cout << "Company Phone: ";
cin >> phone;
}
void display()
{
cout << "Company Name: " << cname << endl;
cout << "Company Address: " << caddress << endl;
cout << "Company Phone Number: " << phone << endl;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Object Oriented Programming Questions and Answers and more Exercises Object Oriented Programming in PDF only on Docsity!

Question 1

#include #include using namespace std; class company { protected: string cname; string caddress; int phone; public: void setcname(string a) { cname = a; } void setcadd(string a) { caddress = a; } void setphone(int a) { phone = a; } string getcname() { return cname; } string getcadd() { return caddress; } int getphone() { return phone; } company() {} company(string^ a, string^ b, int^ c) { cname = a; caddress = b; phone = c; } void input() { cout << "Company Name: "; cin >>^ cname; cout << "Company Address: "; cin >>^ caddress; cout << "Company Phone: "; cin >>^ phone; } void display() { cout << "Company Name: "^ <<^ cname <<^ endl; cout << "Company Address: "^ <<^ caddress << endl; cout << "Company Phone Number: "^ << phone^ << endl;

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;

Question 2 (A)

#include using namespace std; class Shape { public: Shape() {} double x; double y; virtual double area() { return 2.3; } virtual void display() { cout << "Value of x: "^ << x <<^ endl << "Value of y: " << y << endl << endl; } virtual void input() { cout << "Enter Value of x: "; cin >>^ x; cout << "Enter Value of y: "; cin >>^ y; }

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; }

Question 2 (B)

#include using namespace std; class Shape { public: Shape() {} double x; double y; virtual double area() { return 2.3; } virtual void display() { cout << "Value of x: "^ << x <<^ endl << "Value of y: " << y << endl << endl; } virtual void input() { cout << "Enter Value of x: "; cin >>^ x; cout << "Enter Value of y: "; cin >>^ y; }

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(); }

Question 3(A)

#include #include using namespace std; class Mammal { protected: string nimi; public: void setname(string a) { nimi =^ a; } virtual void printinformation() { cout << "Name: "^ << nimi <<^ endl; } }; class Human :public Mammal { public: void printinformation() { cout << "Human Name: "^ << nimi; } }; class Dog: public Mammal { public: void printinformation() {

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; }

Question 4

#include using namespace std; class A { protected: int a; int b; public: A() { a = 0; b = 0; } }; // class B : public A - Wrong Format to use. class B: public virtual A // virtual keyword to be used here. {