Order Of Execution Of Constructor Analysis Information Technology Essay

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<iostream>

Using namespace std;

class alpha

{

int p;

public:

alpha(int i)

{

p=i;

cout<<“alpha initializedn”;

}

void show_p(void)

{

cout<<“p=”<<p<<“n”;

}

};

class beta

{

float q;

public:

beta(float j)

{

q=j;

cout<<“beta initializedn”;

}

void show_ q(void)

{

cout<<“q=”<<q<<“n”;

}

};

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=”<<x<<“n”;

cout<<“y=”<<y<<“n”;

}

};

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:

Read also  What Is Systematic Literature Review Information Technology Essay

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<iostream.h>

#include<conio.h>

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”<<endl;

cin>>title>>author_name>>price;

}

void display( )

{

cout<<” Title “<<“t”<<“author”<<“t”<<“price”<<endl;

cout<<title<<“t”<<author_name<<“t”<<price<<endl;

}

};

class book : public publication

{

int pcount;

public:

void getdata( )

{

cout<<“enter the no. of pages”<<endl;

cin>>pcount;

}

void display( )

{

cout<<“number of pages is : “<<pcount<<endl;

}

};

class ebook : public publication

{

float ptime;

public:

void getdata( )

{

cout<<“enter the playing time”<<endl;

cin>>ptime;

}

void display( )

{

cout<<” playing time is : “<<ptime<<endl;

}

};

void main( )

{

clrscr( );

publication p;

book b;

ebook e;

Read also  Competitive Advantages From Its Implementation Information Technology Essay

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<iostream.h>

#include<conio.h>

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.

Read also  The Important Aspects Of Information System Security Information Technology Essay

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<iostream.h>

#include<conio.h>

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:”<<name<<endl;

cout<<“designation of the employee is:”<<designation<<endl;

cout<<“code of the employee is:”<<code<<endl;

}

};

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:”<<experience<<endl;

cout<<“age is:”<<age<<endl;

}

};

void main()

{

clrscr();

A a;

B b;

a.get();

a.show();

b.gets();

b.put();

getch();

}

………….

Order Now

Order Now

Type of Paper
Subject
Deadline
Number of Pages
(275 words)