Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class A { public : Int x; A()

Similar presentations


Presentation on theme: "Class A { public : Int x; A()"— Presentation transcript:

1 Class A { public : Int x; A() Cout<<“default base class constructor”; } A(int i) x=i; Cout<<“one argument base class constructor”; }; Class B:public A public: int y; B(int j) y=j; Cout<<“one argument derived class constructor”;

2 int main() { B b(2); return(0); }

3 Class A { public : Int x; A(int i) x=i; cout<<“one argument base class constructor”; } }; Class B:public A public: int y; B(int j):A(int j) y=j; cout<<“one argument derived class constructor”;

4 Class A { public : A() Cout<<“default base class1 constructor”; } }; Class B B() Cout<<“default base class2 constructor”;

5 Class C:public B,virtual A
{ public: B() Cout<<“ default derived class constructor”; } }; int main() C c; return(0);

6 Pointers to Objects In inheritance pointers can be declared to point to base or derived class . Pointers to objects of base class are type compatible with pointers to objects of the derived class. a base class pointer can point to objects of both the base and derived class. a derived class pointer can not point to objects of the base class.

7 Pointers to Objects

8 Base class pointer to Derived class objects

9 "this" pointer "this" pointer is a pointer, It points to the object for which the member function is called.

10 Polymorphism Polymorphism, a Greek term, means the ability to take more than one form. -operator overloading -function overloading The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance. C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function. There are two types of polymorphism: 1.Compile time polymorphism 2.Run time polymorphism

11 Polymorphism POLYMORPHISM COMPILE TIME RUN TIME FUNCTION OVERLOADING
OPERATOR OVERLOADING VIRTUAL FUNCTIONS

12 BINDING IN C++ In c++ function can be bound at either compile time or runtime. C++ supports two types of binding: 1.Compile time or early binding(static binding) 2.Run time or late binding(dynamic binding)

13 Static or Early Binding

14 Base class pointer to Derived class objects (Late Binding (or) Dynamic Binding )

15 Virtual Functions A virtual function is a function in a base class that is declared using the keyword virtual. Virtual Function is a function in base class, which is overrided in the derived class, and which tells the compiler to perform Late Binding on this function. it knows which function to call based on the actual type of the object

16 Runtime Polymorphism (Dynamic Method Dispatch)

17

18 Pure Virtual Functions
A pure virtual function (or abstract function) is a virtual function  which has declaration with out implementation . A pure virtual function is declared by assigning 0 in declaration.  A pure virtual function is implemented by classes which are derived from an Abstract class.

19 class Base { int x; public: virtual void fun() = 0;//virtual function int getX() { return x; } }; // This class inherits from Base and implements fun() class Derived: public Base int y; void fun() { cout << “fun() in derived class "; } int main(void) Derived d; d.fun(); return 0; }

20

21

22 Constructors and Virtual Functions
Constructors cannot be virtual. In a constructor, the virtual call mechanism is disabled because overriding from derived classes hasn’t yet happened. 

23

24

25

26

27 MCQs

28 Which is used to create a pure virtual function ? a) $ b) =0 c) & d) !

29 Which is also called as abstract class
Which is also called as abstract class? a) virtual function b) pure virtual function c) derived class d) None of the mentioned

30 Where does the abstract class is used
Where does the abstract class is used? a) base class only b) derived class c) both a & b d) None of the mentioned

31 What is meant by multiple inheritance
What is meant by multiple inheritance? a) Deriving a base class from derived class b) Deriving a derived class from base class c) Deriving a derived class from more than one base class d) None of the mentioned

32 Which symbol is used to create multiple inheritance
Which symbol is used to create multiple inheritance? a) Dot b) Comma c) Dollar d) None of the mentioned

33 . Which of the following advantages we lose by using multiple inheritance? a) Dynamic binding b) Polymorphism c) Both a & b d) None of the mentioned

34 Which is referred by pointers to member
Which is referred by pointers to member? a) static members of class objects b) Non-static members of class objects c) Refering to whole class d) None of the mentioned

35 What does derived class does not inherit from the base class
What does derived class does not inherit from the base class? a) constructor and destructor b) friends c) operator = () members d) all of the mentioned

36 What is meant by containership
What is meant by containership? a) class contains objects of other class types as its members b) class contains objects of other class types as its objects c) both a & b d) none of the mentioned

37 Class A { int a; }; Class B { A a1; }

38 Which is the best design choice for using pointer to member function
Which is the best design choice for using pointer to member function? a) Interface b) Class c) Structure d) None of the mentioned


Download ppt "Class A { public : Int x; A()"

Similar presentations


Ads by Google