Week 11-12-13-14

  • Uploaded by: api-3737023
  • 0
  • 0
  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Week 11-12-13-14 as PDF for free.

More details

  • Words: 5,851
  • Pages: 73
Week 11-12-13-14 Outline • Operators and its types • Operators Overloading • Unary operators Overloading • Binary operators Overloading • Introduction to Inheritance • Types of inheritance • Introduction to Polymorphism • Virtual and Pure Virtual Functions • Abstract Base classes and Concrete derived classes

1

Operators and its types • • •

Assignment (=) The assignment operator assigns a value to a variable. a = 5; This statement assigns the integer value 5 to the variable a. The part at the left of the assignment operator (=) is known as the lvalue (left value) and the right one as the rvalue (right value). The lvalue has to be a variable whereas the rvalue can be either a constant, a variable, the result of an operation or any combination of these. The most important rule when assigning is the right-to-left rule: The assignment operation always takes place from right to left, and never the other way: a = b;

Operators and its types (con’t) • • •

• • •

Arithmetic operators ( +, -, *, /, % ) The five arithmetical operations supported by the C++ language are: + (addition), - (subtraction), * (multiplication), /(division), %(modulo) Operations of addition, subtraction, multiplication and division literally correspond with their respective mathematical operators. The only one that you might not be so used to see may be modulo; whose operator is the percentage sign (%). Modulo is the operation that gives the remainder of a division of two values. For example, if we write: a = 11 % 3; the variable a will contain the value 2, since 2 is the remainder from dividing 11 between 3. Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) When we want to modify the value of a variable by performing an operation on the value currently stored in that variable we can use compound assignment operators: Expression is value += increase; a -= 5; a /= b;

Equivalent to value = value + increase; a = a - 5; a = a / b;

Operators and its types (con’t) • •

Increase and decrease (++, --) Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. • Thus: c++; c+=1; c=c+1; are all equivalent in its functionality: the three of them increase by one the value of c. • For example: • Example 1 Example 2 B=3; B=3; A=++B; A=B++; // A contains 4, B contains 4 // A contains 3, B contains 4

Operators and its types (con’t) • •



• • • • • •

Relational and equality operators ( ==, !=, >, <, >=, <=) In order to evaluate a comparison between two expressions we can use the relational and equality operators. The result of a relational operation is a Boolean value that can only be true or false, according to its Boolean result. We may want to compare two expressions, for example, to know if they are equal or if one is greater than the other is. Here is a list of the relational and equality operators that can be used in C++: == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to

Operators and its types (con’t) • Logical operators ( !, &&, || ) • The Operator ! is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its operand is false. Basically, it returns the opposite Boolean value of evaluating its operand.

Operators and its types (con’t) • Conditional operator ( ? ) • The conditional operator evaluates an expression returning a value if that expression is true and a different one if the expression is evaluated as false. Its format is: condition ? result1 : result2 • If condition is true the expression will return result1, if it is not it will return result2. • 7==5 ? 4 : 3 // returns 3, since 7 is not equal to 5. 7==5+2 ? 4 : 3 // returns 4, since 7 is equal to 5+2. 5>3 ? a : b // returns the value of a, since 5 is greater than 3. a>b ? a : b // returns whichever is //greater, a or b.

Operators and its types (con’t) • •

• • • •

Comma operator ( , ) The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is considered. For example, the following code: a = (b=3, b+2); Would first assign the value 3 to b, and then assign b+2 to variable a. So, at the end, variable a would contain the value 5 while variable b would contain value 3. Bitwise Operators ( &, |, ^, ~, <<, >> ) Bitwise operators modify variables considering the bit patterns that represent the values they store. Operator Asm equivalent Description & | ^ ~

Inclusive OR

inversion) << >>

AND OR

Bitwise AND Bitwise

XOR NOT

Bitwise Exclusive OR Unary complement (bit SHL SHR

Shift Left Shift Right

Operators and its types (con’t) • sizeof() • This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of that type or object: a = sizeof (char); • This will assign the value 1 to a because char is a one-byte long type. • The value returned by sizeof is a constant, so it is always determined before program execution.

Operators overloading • The general syntax for defining an operator overloading is as follows: return_type classname :: operator operator symbol(argument) { ………….. statements; }

Operators overloading(con’t) • Thus the above clearly specifies that operator overloading is defined as a member function by making use of the keyword operator. • In the above: • return_type – is the data type returned by the function • class name - is the name of the class • operator – is the keyword • operator symbol – is the symbol of the operator which is being overloaded or defined for new functionality • :: - is the scope resolution operator which is used to use the function definition outside the class.

Unary operator overloading • As the name implies takes operate on only one operand. Some unary operators are namely ++ called as Increment operator, -- called as Decrement Operator, ! not ,  ~, unary minus. • The operator keyword • The keyword operator is used to overload the operator “++” for e.g. void operator ++ () • The return types (void in this case) comes first, followed by the operator itself (++) and finally the argument list enclosed in the parentheses (which are empty here) • The declarator syntax tells compiler to call this member function whenever the ++ operator is encountered, provided the operand (the variable operated on by the ++) is of type class

An example of unary operator overloading • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

// countpp1.cpp // increment counter variable with ++ operator #include

class Counter { private: unsigned int count; public: Counter() { count=1; } int get_oount() { return count; } void operator ++ () { count=count*2; } }; int main() { Counter c1,c2; cout <<“\nc1= “ cout <<“\nc2= “ ++c1; ++c2; ++c2; cout << “\nc1=“ cout << “\nc2=“ return 0; // } // end main

<
<
// count // constructor // return count // increment count

//define and initialize //display //increment c1 //increment c2 //increment c2 //display again

c1=1 c2=1 c1=2 c2=4

Unary operators overloading (con’t) • In the previous example, we create two objects of class Counter c1 and c2 • The counts in the objects are displayed; they are initially 0. then using the overloaded ++ operator, we increment c1 once, and display the resulting values

Binary operator overloading • The arithmetic operators (i.e. +,-,*,/) comparison operators (i.e. <,>,<>) and arithmetic assignment operators (i.e. =,!=, ==) operate on two operands can be overloaded

Binary operator overloading • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

// bover1.cpp // overload + operator #include #include class Exforsys { private: int x; int y; public: Exforsys() //Constructor { x=0; y=0;} Exforsys(int x1, int y1) { x=x1; y=y1; } void getvalue( ) //Member Function for Inputting Values { cout << "\n Enter value for x: "; cin>> x; cout << "\n Enter value for y: "; cin>> y; } void displayvalue( ) //Member Function for Outputting Values { cout <<"value of x is: " << x; cout<< "value of y is: "<
• • • • • • • • • • • • • • • •

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

void main( ) { clrscr(); Exforsys e1,e2,e3; //Objects e1, e2, e3 created cout<<"\nEnter value for Object e1 "; e1.getvalue( ); cout<<"\nEnter value for Object e2 "; e2.getvalue( ); e3= e1 + e2; //Binary Overloaded operator used cout<< "\nValue of e1 is:"; e1.displayvalue(); cout<< "\nValue of e2 is:"; e2.displayvalue(); cout<< "\nValue of e3 is:"; e3.displayvalue(); }

Enter Enter Enter Enter Enter Enter value value value value value value value value value

value for object e1 value for x:2 value for y:3 value for object e2 value for x:4 value for y:5 of e1 is: of x is:2 of y is:3 of e2 is: of x is:4 of y is:5 of e3 is: of x is:6 of y is:20

Binary operators overloading (con’t) • • •





In the previous example, we create three objects of class Exforsys e1, e2 and e3 The variables x and y are initialized with 0 in the objects e1 and e2 The binary overloaded operator ‘+’ is used. In this statement, the argument on the left side of the operator ‘+’, e1, is the object of the class Exforsys in which the binary overloaded operator ‘+’ is a member function The right side of the operator ‘+’ is e2. This is passed as an argument to the operator ‘+’ . Since the object e2 is passed as argument to the operator’+’ inside the function defined for binary operator overloading, the values are accessed as e2.x and e2.y. This is added with e1.x and e1.y, which are accessed directly as x and y. The return value is of type class Exforsys as defined by the above example we multiply e1 and e2 and store in e3, and display the

Introduction to Inheritance • In inheritance the code of existing classes is used for making new classes • In inheritance a new class is written such that it can access or use the members of an existing class is called derived class or child class • The existing class is called base class or parent class • The derived class can use the data members and member functions of the base class • It can also have its own data members and member functions, thus making it larger than base class

Introduction to Inheritance (con’t) Member1

Base class

Member2

Member1 Member2 Derived class

Member3

• In the figure, the arrow shows that the derived class can access the members of base class but the base class cannot access members of it derived class

Categories of inheritance • Single inheritance • Multiple inheritance • In single inheritance, the new class is derived from only one base class • In multiple inheritance, the new class is derived from more than one base classes

Protected Access Specifier • Public members of a class are accessible by all functions in the program • Private members of a class are accessible only by member functions and friend functions of that class • Protected members of the class are accessible by the member functions and friend functions of that class • Difference b/w protected members and private members – The protected members of a base class are, however, accessible by the members of its derived classes but – The private members of the base class are not accessible directly by members of its derived

General syntax for defining a derived class class sub_class_name : specifier base_class_name { ----------members of derived class ----------}; where sub_class_name -> class : (colon) -> specifier -> may protected base_class_name ->

represents name of the derived sets relation b/w the classes represents the access specifier. It be public, private or represents name of the base class

Types of inheritance

Single inheritance

Types of inheritance • Public inheritance • Private inheritance • Protected inheritance

Public inheritance • Public inheritance – The public members of the base class become the public members of the derived class – Thus the objects of the derived class can only access public members (data and functions) of the base class – The protected members of the base class also becomes protected members of derived class or can access protected data members of the base class

General syntax for deriving a public class from base class class sub_class_name : public base_class_name { ------------------------------}; where public inheritance sub_class_name base_class_name

->

specifies the public

->

represents name of the derived class represents name of the base class

->

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

An example of Public inheritance

// public1.cpp // An example of a public inheritance #include

class student { private: char name[15], address[25]; public: void input (void) { cout<< “Enter Name “; cin>>name; cout <<“Enter Address “; cin>>address; } void show (void) { cout<<“\nName: “<>s1; cout<<“Enter marks of sub2 “; cin>>s2; cout<<“Enter marks of sub3 “; cin>>s3; total= s1+s2+s3; } void show_detail (void); };

//declare character arrays

An example of Public inheritance

• • • • • • • • •

34 35 36 37 38 39 39 40 41

void main() { marks mmm; mmm.input(); mmm.inputmarks(); mmm.show(); mmm.show_detail(); getch(); } void marks :: show_detail()

• • • • • •

42 43 44 45 46 48

{ cout<<“\nMarks of 1st sub: “<<s1<<endl; cout<<“Marks of 2st sub: “<<s2<<endl; cout<<“Marks of 3rd sub: “<<s3<<endl; cout<<“Total marks :”<
//defining member function “show_detail” outside the class “marks”

Enter Name shehzad Enter Address hayatabad Enter marks of sub1 30 Enter marks of sub2 20 Enter marks of sub3 10 Name: shehzad Address: hayatabad Marks Marks Marks Total

of 1st sub:30 of 2st sub:20 of 3rd sub:10 marks:60

Public inheritance (con’t) • In the previous example, the class marks is defined as a derived class • The keyword “public” and the name of the base class “student” followed by colon(:) are written while defining derived class • This shows that the objects of the derived class are able to access public members of the base class. It is called public inheritance • The derived class “marks” can access the “input” and “show” member functions of the base class, but cannot access other private members of the base class

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Another example of Public inheritance

// public2.cpp // An example of a public inheritance #include

class A { private: int a1,a2; protected: int p1,p2; public: void ppp(void) { cout<<”Value of p1 of class A= “<>p1; //class B access protected data member p1 cout<<“Enter value of p2 ? ”; cin>>p2; //class B access protected data member p2 } }; int main() { B ob; ob.get(); ob.ppp(); //class B object “obj” access public member function “ppp” of class A return 0; } // end main

// indicates successful termination

Enter Enter Value Value

value value of p1 of p2

of of of of

p1 ? 2 p2 ? 3 class A=2 class A=3

Public inheritance (con’t) • In the previous program, the class “B” is publicly derived from class “A” • The objects of the class “B”: – Cannot access the private data members “a1” & “a2” of base class “A” – Can access the public member function “ppp” of base class “A” using its object “ob” – Can access the protected data members “p1” & “p2” of base class “A”

Private inheritance • Private inheritance – The objects of the derived class cannot access public members and private members (data and functions) of the base class – It can only access protected members of the base class

General syntax for deriving a private class from base class class sub_class_name : private base_class_name { ------------------------------}; where private inheritance sub_class_name base_class_name

->

specifies the private

->

represents name of the derived class represents name of the base class

->

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

An example of Private inheritance

// private1.cpp // An example of a private inheritance #include

class A { private: int a1,a2; public: void ddd() { cout<<“cannot access public member function”; } protected: int p1,p2; void ppp(void) //protected member function { cout<<”Value of p1 of class A= “<>p1; cout<<“Enter value of p2 ? ”; cin>>p2; ppp(); //call protected member function ppp() } }; int main() //ppp() is not acessible in main because it is protected { B ob; ob.get(); ob.ddd(); //ddd() is not accessiable return 0; // indicates successful termination } // end main

Enter Enter Value Value

value value of p1 of p2

of of of of

p1 ?2 p2 ?3 class A=2 class A=3

Private inheritance (con’t) • In the previous program, the class “B” is derived as private from the base class “A” • The objects of the class “B”: – Cannot access the private data members “a1” & “a2” of base class “A” – Cannot access the public member function “ddd” of base class “A” – Can access the protected data members “p1” & “p2” of base class “A” – Can access the protected member function “ppp” of base class “A”

Protected inheritance • Protected inheritance – The objects of the derived class that is derived as protected can access only the protected members of the base class

General syntax for deriving a protected class from base class class sub_class_name : protected base_class_name { ------------------------------}; where protected inheritance sub_class_name base_class_name

->

specifies the protected

->

represents name of the derived class represents name of the base class

->

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

An example of Protected inheritance

// protected1.cpp // An example of a protected inheritance #include

class A { private: int a1,a2; protected: int p1,p2; public: void ppp(void) { cout>>”Value of p1 cout>>”Value of p2 class B : protected A { public: void get(void) { cout<<“Enter value cout<<“Enter value cout>>”Value of p1 cout>>”Value of p2 void main() { B ob; ob.get(); return 0; } // end main

of class A= “<
of of of of

p1 ? ”; cin>>p1; p2 ? ”; cin>>p2; class A= “<
// indicates successful termination

Enter Enter Value Value

value value of p1 of p2

of of of of

p1 ?2 p2 ?3 class A=2 class A=3

Protected inheritance (con’t) • In the previous program, the class “B” is derived as protected from the base class “A” • The objects of the class “B”: – Can only access the protected data members “p1” & “p2” of base class “A”

Types of inheritance

Multiple inheritance

Multiple inheritance (con’t) • In multiple inheritance, a class is derived by using more than one classes • In multiple inheritance, the derived class receives the members of two or more base classes • Multiple inheritance is a powerful feature of OOP • This technique reduces the program size and saves programmer’s time • The programmer uses the existing base classes in the program coding to solve problems

Multiple Inheritance (con’t) Member1

Member1

Member1

Member2

Member2

Member2

Base class A

Base class B

Base class C

Member1 Member2 Derived class D

Member3

• In the figure, the arrow shows that the Derived class D can access the members of base class A, B and C respectively, but the base classes cannot access members of it derived class

General syntax for defining a derived class from multiple base classes class sub_class : specifier base_class1, specifier base_class2, … { ----------members of derived class ----------}; where sub_class_name -> represents name of the derived class : (colon) -> sets relation b/w the classes specifier base_class1 -> represents the access specifier of the first base class. specifier base_class2 -> represents the access specifier of the second base class.

• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

An example of Multiple inheritance

// multiple1.cpp // An example of a multiple inheritance #include

class student { private: char name[15], address[15]; //declare character arrays public: void input (void) { cout<< “Enter name: “; cin>>name; cout <<“Enter Address: “; cin>>address; } void print (void) { cout<< “Name: “<>s1; cout<<“Enter marks of sub2: “; cin>>s2; cout<<“Enter marks of sub3: “; cin>>s3; total= s1+s2+s3; }

• • • • • • • • • • • • • • • • • • • • • • • • • • •

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

void showmarks(void) { cout<<“Marks of 1st sub: “<<s1<<endl; cout<<“Marks of 2st sub: “<<s2<<endl; cout<<“Marks of 3rd sub: “<<s3<<endl; cout<<“Total marks :”<
Enter Enter Enter Enter Enter

name: shehzad Address: hayatabad marks of sub1:52 marks of sub2:63 marks of sub3:85

The complete record is ========================== Name: shehzad Address: hayatabad Marks of 1st sub:52 Marks of 2nd sub:63 Marks of 3rd sub:85

Introduction to Polymorphism • In polymorphism, the member functions with the same name are defined in each derived class and also in the base class • Polymorphism is used to keep the interface of base class to its derived classes • “Poly” means many and “morphism” means form • Polymorphism is achieved by means of virtual functions

Polymorphism (con’t) • Pointers to objects • The members of a class can be accessed through the pointer to the class • The arrow (->) symbol is used to access them • This symbol is also known as member access operator • The general syntax to access a member of a class through its pointer is: p->class member where p is the pointer to the object -> it is the member access operator. It is used to access the members of the object through the pointer class member it is the member of the object

• • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

An example of polymorphism

// poly1.cpp // An example of a polymorphism #include

class bb { public: void ppp() { cout<<“It is the base class”<<endl; } }; class d1 : public bb { public: void ppp() { cout<<“It is the first derived class”<<endl; } }; class d2 : public bb void ppp() { cout<<“It is the second derived class”<<endl; } };

• • • • • • • • • • •

29 30 31 32 33 34 35 36 37 38 39

int main() { bb *p; d1 a; d2 b; p = &a; p-> ppp(); p = &b; p-> ppp(); return 0; // indicates successful termination } // end main

It is the base class It is the base class

Polymorphism (con’t) • • • • •

• •

In the previous example, one pointer object “p” of base class “bb” is declared One object “a” of derived class “d1” is declared One object “b” of derived class “d2” is declared The statement “p=&a” assigns address of object “a” to the pointer object “p” When p->ppp() is executed, the member function of the base class is executed, since the type of the pointer matches the type member function of the base class, the base class member function is executed Similarly, the p->ppp() is executed after assigning the address of the object “b” of the derived class “d2” to “p”, again the member function of the base class is executed Accessing member functions of a class through the pointer is example of early-binding or static binding. i.e. class type matches the type of the pointer irrespective of the contents of the pointer

Virtual and pure virtual functions • A virtual function is a special type of member function • It is defined in the base class and may be redefined in any class derived from this base class • Its name in the base class and in the derived classes remains the same • Its definition in these classes may be different • The virtual function in derived class is executed through pointer of the public base class • A virtual function is declared by writing the word “virtual” before function declaration in the base class • The use of the word “virtual” before function declaration in derived classes is optional • In virtual function execution, late binding or dynamic binding occurs, because the compiler selects the function to be called based on the contents of the pointer and not on the basis of the type of the pointer

Virtual and pure virtual functions (con’t) • Once a function is declared virtual in a base class, it becomes virtual in all derived classes, even the keyword virtual is not written in its definition in the derived classes • The derived classes may have different versions of a virtual function • A redefined function in derived classes is said to override the base class function • In executing virtual function, the complier selects the function to be called based on the contents of the pointer (i.e. address) and not on the basis of the type of the pointer (i.e. class) • Without using keyword virtual, function to be called based on the contents of the pointer (i.e. class) and not on type of pointer (i.e. address)

• • • • • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

An example of Virtual functions

// vfunction1.cpp // An example of a virtual function #include

class bb { public: virtual void ppp() { cout<<“It is the base class”<<endl; } }; class d1 : public bb { public: void ppp() { cout<<“It is the first derived class”<<endl; } }; class d2 : public bb {public: void ppp() { cout<<“It is the second derived class”<<endl; }

• • • • • • • • • • •

30 31 32 33 34 35 36 37 38 39 40

int main() { bb *p; d1 a; d2 b; p = &a; p-> ppp(); p = &b; p-> ppp(); return 0; // indicates successful termination } // end main

It is the first derived class It is the second derived class

Virtual functions (con’t) • In the previous example, the keyword “virtual” appears only in the definition of the base class “bb”. • Each derived class “d1” and “d2” and base class “bb” have a member function with the same name, i.e. ppp() • The statement “p=&a” assigns address of object “bb” to the pointer object “p” • When p->ppp() is executed, the pointer object of the base class contains the address of the object of the derived class “d1” and the virtual member function of the derived class “d1” is executed • Similarly, when the p->ppp() is executed the pointer object “p” of the base class contains the address of the derived class “d2” and the virtual member function of the derived class “d2” is executed • In executing virtual function, the compiler selects the function to be called based on the contents of the pointer and not on the basis of the type of the pointer

Pure virtual function • The virtual function that is only declared but not defined in the base class is called the pure virtual functions • A function is made pure virtual by preceding its declaration with the keyword “virtual” and by postfixing it with = 0 • The class that contains the pure virtual function exists only to act as a parent or base of the derived class • The base class that has one or more pure virtual functions is called the Abstract base class • An object of abstract class cannot be defined.

Virtual function guidelines • A member function of the base class to be overridden should be declared with keyword “virtual” • In the presence of virtual member function in the base class, the destructors of that class should also be declared as virtual. The reason is that the correct destructor will remove or clean it up • Calling an overriding function, supply of the same member and type of parameters must match the number and type in the declaration of the function in the base class

Virtual function properties and reasons • It must be a member function of a base class • It must start with the keyword “virtual” • A call to a virtual function is resolved during run-time, this mechanism is also known as late binding

• • • • • • • • • • • • • • • • • • • • • • • • •

1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 20 21 22 23 24 25 26 27 28

An example of pure virtual functions // pvfunction1.cpp // An example of a pure virtual function #include

class bb { public: virtual void ppp() = 0; }; class d1 : public bb { public: void ppp() { cout<<“First Derived class”<<endl; } }; class d2 : public bb void ppp() { cout<<“Second Derived class”<<endl; } };

• • • • • • • • • • •

29 30 31 32 33 34 35 36 37 38 39

void main() { bb *p; d1 a; d2 b; p = &a; p-> ppp(); p = &b; p-> ppp(); return 0; // indicates successful termination } // end main

First Derived class Second Derived class

Abstract Base classes and Concrete derived classes • The base class that has one or more pure virtual functions is called the Abstract Base class • These classes (Abstract base classes) designed as a framework for derived classes • An abstract base class cannot be instantiated. i.e. an object of its type cannot be defined. But a pointer to an abstract base class can be defined • The derived class that does not have any pure virtual function is called Concrete Derived class • The derived classes usually have their own

Abstract Base classes and Concrete derived classes (con’t)

• In OOP a hierarchy of classes are defined • The Abstract Base class is defined at the top of this hierarchy • The Concrete Derived classes are derived from Abstract Base class • The pure virtual functions are declared in the base class • Each derived class contains its own version of these pure virtual functions • In this way a uniformity is achieved within the hierarchy of the class

Related Documents

Week
November 2019 33
Week
December 2019 27
~week~
November 2019 33
Week By Week Schedule
May 2020 20
Week 35
May 2020 0
Week 29
May 2020 3