Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

HOMEWORK No: 3

Name: Ankur srivastava Roll no.: C6912B49 Course: Object Oriented Programming Code: CSE202 Date of Submission: Today

ANS-1When we create a derived class object then firstly the control goes to derived class constructor and then it takes arguments(if it has arguments) from the derived class constructor and then control goes to base class constructor with arguments . After that it executes the body of base class constructor and control goes to derived class constructor and executes the body of derived class constructor. Let me clear with an suitable example:-

Class ABC { Protected: Int a; Public: ABC() { a=2; Cout<<endl<<a; } }; Class XYZ:public ABC { Int b; Public: XYZ():ABC() b=1; cout<<endl<<b; } }; Void main() { XYZ a1; } The output of above program is 2 1 The above case is the case of single inheritance with no base and derived constructor arguments. In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of derived class. So constructors are also called according to declaration of base classes and then derived class constructor is being called. Similarly in case of a multiple inheritance , the constructors will be executed in the order of inheritance. For example:Class base { Public:

base() { Cout<<endl<<third; } }; Class derived1:public base { Public: derived1():base() { Cout<<endl<<second; } }; Class derived2:public derived1 { Public: derived2():derived1() { Cout<<endl<<first; } }; Void main() { Derived2 d; } OUTPUT is Third Second First

Ans-2Generally we know that a private member of a base class is not inheritable. But there is a way through which we can access the private data members of base class. We can access the private members of base class through the member functions of base class by making these member function public in derived class. Let me clear with a suitable example:-

Class A { Int a; Public: Void get() { Cout<<enter value of a; Cin>>a; } }; Class B:public A { Int b; Public: Void get1() { Cout<<enter the value of b; Cin>>b; } Void display() { Void get(); Cout<<b; } }; Void main() { B b1; b1.get1(); b1.display(); }

OUTPUT Enter value of a 2 2 Enter value of b 3

From above example it is clear that we can access private data members of base class without modifying it.

ANS-3#include<iostream.h> #include<conio.h> Class publication { Int price; Char athname[10]; Char title[10]; Public: Void getdata() { Cout<<Enter the title of book; Cin>>title; Cout<<enter the author name; Cin>>athname; Cout<<enter the price; Cin>>price; } Void display() { Cout<<title and author of book is<<title<<endl<<athname; Cout<<price of book is<<price; } }; Class book:public publication { Int pcount; Public: Void getdata() { Cout<<enter the no. of pages of that book Cin>>pcount; } Void display()

{ cout<<no. of pages of that book is <<pcount; } }; Class ebook:public publication { Int ptime; Public: Vpid getdata() { Cout<<enter the playing time of cd; Cin>>ptime; } Void display() { Cout<<playing time of cd is<<ptime; } }; Void main() { Int I; Book b1[10]; Ebook b2[10]; For(i=0;i<10;i++) { b1[i].publication::getdata(); b1[i].getdata(); b1[i].publication::display(); b1[i].display(); } For(i=0;i<10;i++) { b2[i].publication::getdata(); b2[i].getdata(); b2[i].publication::display(); b2[i].display(); } getch(); }

ANS-4- Polymorphism allows the same function to act differently in different class . As w e know that polymorphism are of two types a) Compile time polymorphism:- This type of polymorphism is achieved at compile time. It generally includes 1)function overloading 2) operator overloading. In this type of polymorphism the overloaded member functions are selected for invoking by matching arguments , both type and number. This information 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. This is also known as early binding or static binding or static linking. Example of function overloading is given below #include<iostream.h> #include<conio.h> Class abc { Int a; Int b; Int c; Int sum; Public: Void sum(int a, int b) { Sum =a+b; Cout<<sum; } Void sum(int a, int b, int c) { Sum=a+b+c; Cout<<sum; } }; Void main() { abc a1, a2; a1.sum(2,3); a2.sum(3,4,5); getch(); }

OUTPUT 5 12 Example of operator overloading #include<iostream.h> #include<conio.h> Class abc { Int x, y, z; Public: Void getdata(int a,int b,int c) { x=a; y=b; } Void display() { Cout<<x<<y<<z; } Void operato-(); }; Void abc::operator-() { x=-x; y=-y; z=-z; } Void main() { abc s; s.getdata(10,-20,30); cout<<S :; s.display(); -s; Cout<<S :; S.display(); }

OUTPUT S : 10 -20 30 S: -10 20 -30 b) Run time polymorphism:- Run time polymorphism can be achieved using virtual functions. When a function is made virtual, C++ determines which function to use at run time based on the type of object pointed to by the base pointer, rather than the type the pointer.
For example:-

class Base { Public: Virtual void display() { Cout << show base << endl : } }; Class Derived : public Base { Public : void display() { Cout << show Derived << endl : } }; Void main () { Base *p; Base b; Derived d; p = &b; p->display(); p=&d; p->display(); getch(); }

OUTPUT

Show base Show derived

The concept of virtual function helps in achieving polymorphism. In virtual function mechanism, any derived class function can be called using Base class pointer. Function declared inside that particular class will be called of which object is assigned to base class pointer. class Base { Public: Virtual void PrintClassName() { Cout << show base << endl : } }; Class LevelOneDerived : public Base { Public : void PrintClassName() { Cout << LevelOneDerived << endl : } }; Void main () { Base *pBasePointer ; Base baseObject; LevelOneDerived levelOneObject; pBasePointer = &baseObject; pBasePointer->PrintClassName(); pBasePointer =&levelOneObject; pBasePointer->PrintClassName(); } OUTPUT Show base LevelOnederived

ANS-5- We make a virtual function pure when we want the base class to present only an interface for its derived classes. That is, we dont want anyone to actually create an object of the base class, only to upcast to it so that its interface can be used. This is accomplished by making that class abstract, which happens if you give it at least one pure virtual function. We can recognize a pure virtual function because it uses the virtual keyword and is followed by = 0. If anyone tries to make an object of an abstract class, the compiler prevents them. This is a tool that allows you to enforce a particular design. Creating a pure virtual function allows you to put a member function in an interface without being forced to provide a possibly meaningless body of code for that member function. Example of pure virtual function is:-#include <iostream> using namespace std; class Pet { public: virtual void speak() const = 0; virtual void eat() const = 0; }; class Dog : public Pet { Public: void eat() { cout << "Pet eat()" << endl; } void speak() { cout << "Pet speak()" << endl; }

};

void main() { Dog A1; A1.speak(); A1.eat();

ANS-6
#include<iostream.h> Class A { Public: Char name[10]; Int code; Char designation[10]; }; Class B:public virtual A { Private: Int experience; Int age; Public: Void input() { Cout<<enter your name; Cin>>name; Cout<<enter code; Cin>>code; Cout<<write your designation; Cin>>designation; Cout<<enter your experience; Cin>>experience; Cout<<enter your age; Cin>>age; } Void display() { Cout<<name; Cout<<code; Cout<<designation; Cout <<experience; Cout<<age; } }; Void main() { B one; One.input();

One.display(); getch(); }

You might also like