Anda di halaman 1dari 14

# HOMEWORK No.

Course : Object Oriented Programming Code : CSE202

Date of Assignment : Date of Submission :

PART A

Q1. In what order are the class constructors called when a derived class object is
created?

If the base class has constructors that are to be called then according
to the Syntax shown below:

Derived_class _name():Base_class_name()

We can call the base class constructors as well.Therefore first the base
class constructor is executed and then the derived class constructor is
called.

#include<iostream.h>

#include<conio.h>

class A

int x;

public:

A(int i)

x=i;

cout<<"\nA is initialized";

void show_x()
{

cout<<"\nx="<<x;

};

class B

float y;

public:

B( float j)

y=j;

cout<<"\nB is initialized";

void show_y()

cout<<"\ny="<<y;

};

class C:public A,public B

int m,n;

public:

C( int a, float b, int c, int d):A(a),B(b)

m=c;
n=d;

cout<<" \nC is initialized";

void show_z()

cout<<"\nm="<<m;

cout<<"\nn="<<n;

};

void main()

clrscr();

C c(4,5,6,7);

c.show_x();

c.show_y();

c.show_z();

getch();

b) Class D is derived from class B. The class D does not contain any data members

of its own. Does the class D require constructors? If yes, why?

If a base class does not have any constructor then derived class need
not have a constructor function. If a base class contains any
constructor function with any argument then it is mandatory for the
derived class to have a constructor and pass the arguments to the
base class constructor when both classes have constructor then base
class will execute firstly and then derived class will be executed.
Q2. We know that a private member of a base class is not inheritable. Is it anyway
possible for the objects of a derived class to access the private members of the base
class? If yes,how? Remember the base class cannot be modified.
ANSWER:

We know that the private members of a class can only be achieved


through the member functions of that class.

But this can be accomplish by modifying the visibility mode of the


private member by making it public but there will be the disadvantage
of this, which is that this would we accessible to all other function of
the program, thus taking away the advantage of data hiding.

We know C++ provide a third mode protected. Protected part of class


is accessible by member function within its class and any class
immediately derived from it.

Thus ,if we want to access the private members of the base class
which cannot be inherited ,we need to call the member function of the
base call through the member function the derived class as shown in
the example below:

Example: 1

#include<iostream.h>

#include<conio.h>

Class A

int a;

public:

void show()

a=10;

cout<<”The value of a is”<<a;

};

Class B:public A
{

public:

void aces( )

A::showdata( );

};

void main()

clrscr( );

B obj;

obj.aces( );

getch( );

Q3. Assume that a publishing company prints books and digital books(electronic
form- CD). Create a class named publication with data members title, price, and
author’s name. From publication class, derive two classes named book and ebook. Te
book class adds a page count data member named pcount while ebook adds data
member playing time name ptime.Each of these classes must have member function
getdata( ) to read class specific data from keyboard and displaydata( ) to output the
class specific data to the computer screen.

#include<iostream.h>

#include<conio.h>

class publication

int price;

char n[10];
char t[20];

public:

void getdata()

cout<<”enter the name, title and price”;

cin>>n>>t>>p;

Void display()

cout<<”the name, title and price are”<<n<<t<<p;

};

class book:public publication

private:

Int pcount;

public:

void getdata()

cout<<”enter the pcount”;

cin>>pcount;

void displaydata()

display();
cout<<”the pcount is”<<pcount;

};

class ebook : public publication

private:

int ptime;

public:

void getdata();

cout<<”enter the ptime”;

cin>>ptime;

void displaydata()

cout<<”the ptime is”<<ptime;

};

void main()

book b;

ebook e;

b.getdata();

e.getdata();

b.displaydata();
e.displaydata();

getch();

PART B

Q4. How is polymorphism achieved at


(a) compile time (b) run time?
(b) How does the concept of virtual function help in achieving polymorphism?
Illustrate your answer by giving a suitable example.

(a) Function Overloading and Operator Overloading

The overloaded functions are selected for invoking by matching


arguments. This is known to compiler at the compile time and
therefore compiler is able to select the appropriate function for a
particular call at the compile time itself which is called early binding or
static binding.

(a) Consider the following example

Ex.

class A

int x;

public:

void show()

};
class B:public A

int y;

public:

void show();

};

since the function show() is same in both the classes therefore static
binding does not apply. Therefore member function could be selected
while the program is running.

(b)

When we use the same function name in both the base class and
derived class, the function in base class is declared as virtual using the
keyword virtual. When the function is made virtual then C++
determines which function is to use at runtime, based on the type of
object pointed to by the base pointer. Thus by making the base pointer
to point different objects we use different versions of virtual functions.

#include<iostream.h>

#include<conio.h>

class Base

public:

virtual void getdata()=0;

};

class Drive:public Base

public:

void getdata()

cout<<"\nDrive class";
}

void showdata()

cout<<"\nDisplay derive";

};

void main()

clrscr();

Drive d;

Drive *p;

p=&d;

p->getdata();

p->showdata();

getch();

Q5. When do we make a virtual function “pure”? What are the implications of
making a

function a pure virtual function?

ANS 5

We make the virtual function pure because compiler requires the each
derived class to either define the function or redeclare it as a pure
virtual function. A class containing the pure virtual function cannot use
to declare the object of its own.

Example :

#include<iostream.h>

#include<conio.h>
class Base

public:

virtual void getdata()=0;

};

class Drive:public Base

public:

void getdata()

cout<<"\nDrive class";

void showdata()

cout<<"\nDisplay derive";

};

void main()

clrscr();

Drive d;

Drive *p;

p=&d;

p->getdata();

p->showdata();

getch();
}

Q6. Develop an object oriented program in C++ to read the following information
from the keyword in which the base class consists of employee name, code and
designation, and the derived class containing the data members, viz. years of
experience and age.

a) Employee name
b) Employee code
c) Designation
d) Years of experience
e) Age

ual base class for the item employee name and code.

Design a virt Design a virtual base class for the item employee
name and code.

ANS 6

#include<iostream.h>

#include<conio.h>

Class A

int emp_code;

char emp_name[10];

char desig;

public:

void getdata()

cout<<”enter the name , code and designation”;

cin>>n

cin>>c
cin>>d;

void display()

cout<<”the name ,code and designation is”<<n<<c<<d;

};

Class B: virtual public A

int a;

int y;

public:

void getdata()

A::getdata();

cout<<”enter the age and years of experience”;

cin>>a

cin>>y;

void display1()

A::display();

cout<<”the age and years of experience are”<<a<<y;

};
void main()

B obj;

Obj.getdata1();

Obj.display1();

getch();

Anda mungkin juga menyukai