Disclaimer: This is an example of a student written essay.
Click here for sample essays written by our professional writers.

Any opinions, findings, conclusions or recommendations expressed in this material are those of the authors and do not necessarily reflect the views of UKEssays.com.

Order Of Execution Of Constructor Analysis Information Technology Essay

Paper Type: Free Essay Subject: Information Technology
Wordcount: 1745 words Published: 1st Jan 2015

Reference this

When the constructor is defined in base class and it is a default constructor, then there is no need to define constructor in derived class.

When the constructor defined in the base class in the parameterised, then the constructor in derived class is also parameterised.

First the constructor of the base class will be executed and then of derived class.

The order of execution is first of all the constructor of virtual class will be executed and then of base class and then at last of the derived class.

(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?

Answer: Yes , class D requires constructor even if it doesn’t contain any data member of its own. Derived class constructor is used to provide the values to the base class constructor.

Example-

#include

Using namespace std;

class alpha

{

int p;

public:

alpha(int i)

{

p=i;

cout<<"alpha initializedn";

}

void show_p(void)

{

cout<<"p="<}

};

class beta

{

float q;

public:

beta(float j)

{

q=j;

cout<<"beta initializedn";

}

void show_ q(void)

{

cout<<"q="<}

};

class gamma: public beta , public alpha

{

int x,y;

public:

gamma(int a, float b, int c, int d):

alpha (a),beta(b)

{

x =c;

y =d;

cout<<"gamma initializedn";

}

viod show_xy(void)

{

cout<<"x="<cout<<"y="<}

};

int main( )

{

gamma g(5,10.75,20,30);

cout<<"n";

g.show_p( );

g.show_q( );

g.show_xy( );

getch( );

}

Question 2: 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: No, it is not possible to access the private data members of the base class irrespective of whether the derived class has been inherited publicly or privately.For example, in the following program the declaration for accessing the private data members of the base class by the public members of the derived class is invalid:

CASE 1: A derived class with private derivation.

class baseA{

private:

int value;

};

class derived : private baseA{

public:

void f()

{

++value;

};

};

CASE 2: Even if the derived class has been derived publicly from the base class, the public members of the derived class cannot access the private member of the base class.

class baseA{

private:

int value;

};

class derivedB : public baseA{

public:

void F(){

++value;

};

};

A derived class public member function cannot access the private member of a base class irrespective of whether the derived class has been derived publicly or privately.The above program shows an error message indicating that baseA :: value is not accessible.

Question 3: 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.

Answer .

#include

#include

class publication

{

Char title[50], author_name[50];

Float price;

public:

void getdata( )

{

cout<<"enter the book title, name of author and price of book"<cin>>title>>author_name>>price;

}

void display( )

{

cout<<" Title "<<"t"<<"author"<<"t"<<"price"<cout<}

};

class book : public publication

{

int pcount;

public:

void getdata( )

{

cout<<"enter the no. of pages"<cin>>pcount;

}

void display( )

{

cout<<"number of pages is : "<}

};

class ebook : public publication

{

float ptime;

public:

void getdata( )

{

cout<<"enter the playing time"<cin>>ptime;

}

void display( )

{

cout<<" playing time is : "<}

};

void main( )

{

clrscr( );

publication p;

book b;

ebook e;

p.getdata();

p.display();

b.getdata();

b.display();

e.getdata();

e.display();

getch();

}

PART-B

Question 4:

(a) How is polymorphism achieved at (a) compile time (b) run time?

Answer: Polymorphism is the process of defining a number of objects of different classes into a group and calls the methods to carry out the operation of the objects using different function calls.

Polymorphism can be achieved in two ways one at compile time and second at run time.

Polymorphism

Compile time:

Function overloading, operator overloading

Run time:

virtual functions

Compile time polymorphism:-

This information is known to the compiler at compile time at that time the compiler is able to select the appropriate function for a particular call. This polymorphism uses overloaded operators and member functions. This polymorphism is achieved at compile time.

Run time polymorphism:-

When it is known what objects are under consideration at run time and the appropriate function is invoked. Since the function is linked with a particular class much later after the compilation, this process is called run time polymorphism.Run time polymorphism is achieved by using virtual functions and it is achieved while the program is running.

(b) How does the concept of virtual function help in achieving polymorphism?

Illustrate your by giving a suitable example.

Answer: This can be illustrated as follows:-

#include

#include

class baseA

{

public:

virtual void display( )

{

cout<<"one n";

}

};

class derivedB : public baseA

{

public:

virtual void display( )

{

cout<<"two n";

}

};

class derivedC : public baseA

{

public:

virtual void display( )

{

cout<<"three n";

}

};

void main( )

{

baseA obja;

derivedB objb;

derivedC objc;

baseA *ptr[3];

ptr[0]=&obja;

ptr[1]=&objb;

ptr[2]=&objc;

for (int i=0;i<=2;i++)

ptr[i]->display( );

getch();

}

Question 5: When do we make a virtual function “pure”? What are the implications of making a function a pure virtual function?

Answer : A pure virtual function or pure virtual method is a virtual function that is required to be implemented by a derived class that is not abstract. Classes containing pure virtual methods are termed “abstract;” they cannot be instantiated directly, and a subclass of an abstract class can only be instantiated directly if all inherited pure virtual methods have been implemented by that class or a parent class.

Although pure virtual methods typically have no implementation in the class that declares them, pure virtual methods in C++ are permitted to contain an implementation in their declaring class, providing fallback or default behaviour that a derived class can delegate to if appropriate.

Pure virtual functions are also used where the method declarations are being used to define an interface for which derived classes will supply all implementations. An abstract class serving as an interface contains only pure virtual functions, and no data members or ordinary methods. Use of purely abstract classes as interfaces works in C++ as it supports multiple inheritance.

Question 6: 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.

Employee name

Employee code

Designation

Years of experience

Age

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

Answer :

#include

#include

class A

{

Char name[20],designation[50];

int code;

public:

void get()

{

cout<<"enter name of the employee";

cin>>name;

cout<<"enter designation of the employee";

cin>>designation;

cout<<"enter code of the empsloyee";

cin>>code;

}

void show()

{

cout<<"name of the employee is:"<cout<<"designation of the employee is:"<cout<<"code of the employee is:"<}

};

class B:virtual public A

{

int experience,age;

public:

void gets()

{

cout<<"enter age of experience of the employee";

cin>>experience;

cout<<"enter age of the employee";

cin>>age;

}

void put()

{

cout<<"age of experience is:"<cout<<"age is:"<}

};

void main()

{

clrscr();

A a;

B b;

a.get();

a.show();

b.gets();

b.put();

getch();

}

………….

 

Cite This Work

To export a reference to this article please select a referencing stye below:

Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.
Reference Copied to Clipboard.

Related Services

View all

DMCA / Removal Request

If you are the original writer of this essay and no longer wish to have your work published on UKEssays.com then please: