Advance C++

1. Computer Programming 2. History of Prog.Language 3. Introduction to C++ 4. OOPS 5. Features of OOPS 6. Evolution of C++ 7. Features of C++ 8. Starting Turbo C++ 9. C++ Tokens 10. Basic requirment of C++ 11. Comments 12. My First Program in C++ 13. Line Break 14. C++ Escape Sequence 15. Constants or Literals 16. Variable in C++ 17. Data Types in C++ 18. Basic/Fundamental/Built-in data types 19. User defined data types 20. Derived data types 21. Range of data types in C++ 22. Declare Variables 23. Reserved Key Words 24. cout<< 25. cin>> 26. Prg.to print sum & average 27. Area & Perimeter of Rectangle 28. Area Circumference of Circle 29. Prg. for Area of Triangle 30. Prg.for Simple Calculator 31. Prg.for Arithmetic operators on char variables 32. Calculate Cubic feet & Gallons of water tank 33. setw(n) function 34. setprecision(n) function 35. Operators in C++ 36. Ex. Of Operators 37. Chained and embedded statement 38. C++ prg. to convert farenheit to centigrade/celsius 39. C++ prg. to calculate Area and third side of triangle 40. C++ prg. to solve quadratic equation 41. C++ prg. to solve simultaneous equations 42. C++ prg. for Unary pre and post increment and decrement 43. C++ prg. for reverse a two digit number 44. C++ prg. for Swap two Numbers 45. Hierarchy of operator 46. If Statement 47. C++ prg. for print larger no of two. 48. if..else statement 49. C++ Prg. to print larger no using if..else. 50. C++ Prg. to check divisibility 51. C++ prg. to check leap year 52. C++ prg.to check triangle 53. Prg.to check Equilateral, Isosecles, Scalene triangle 54. Prg.to Calculate Coefficients of quadratic equation 55. C++ if. Else if program example 56. for. . . loop statement 57. prg.to print series using for..loop 58. Prg.to check divisibility 59. prg.to print sum of series 60. C++ prg.for Sum of Factorial 61. C++ prg.to add each digit 62. C++ prg.to print table for that number 63. C++ prg.to print ASCII chart for upper and Lower case 64. C++ prg.to print largest no and smallest no of all. 65. C++ prg.to print no valid or not and print the no. of days 66. C++ prg.to print the distance covered in every 5 seconds 67. C++ prg.to show the distance covered by airplane in the intervals of 20 seconds 68. C++ prg.to print Fibonacci seriess 69. C++ prg.to print factors, gcd and lcm 70. C++ prg.to print four digit perfect squares 71. C++ prg.to calculates the sine of the angle 72. C++ prg.to calculate simple and compound interest 73. C++ prg.to calculate simple and compound interest 74. Points to keep in mind for pyramid 75. C++ Pyramid Prg. Example for Type1 76. C++ Pyramid Prg. Example for Type2 77. C++ Pyramid Prg. Example for Type3 78. C++ Pyramid Prg. Example for Type4 79. C++ Pyramid Prg. Example for Type5 80. C++ prg.to fill screen 81. prg.for largest/smallest no 82. prg.to print nos i,j,k,l such that i 83. while loop statement 84. prg.to print GCD and LCM using while 85. C++ prg.for Prime numbers 86. C++ prg. to check whether a number is palindrome or not 87. C++ prg. to print maximum and minimum number 88. C++ prg. to print Reverse no 89. C++ prg. to calculate factorial using while loop 90. C++ prg. for Armstrong number 91. do. . While statement 92. goto statement 93. break & continue statement 94. switch,case,default statement 95. C++ prg. to enter month number and print month in character 96. C++ prg. to enter character and check is it Vowel or not 97. C++ prg. to implement a calculator using switch case 98. C++ prg. to calculate sine,cosine or tangent of the angle using switch case 99. Array in C++ 100. Declaring Single Dimensional Array in C++ 101. C++ prg.to print nos in original and reverse sequence using Array 102. Ascending/Descending order 103. C++ prg.to search no in Array 104. Diff. between Linear and Binary Serach in Array 105. C++ prg.to rotate an array of n numbers to the right by 1. 106. C++ prg.to Rotate an Array to the left by 1 107. C++ prg.to insert element in the array 108. C++ prg.to delete element in the array 109. Double Dimension Array in C++ 110. C++ prg.to Enter and print using Double Dimensional Array 111. C++ prg.to input a matrix and print it using two dimensional arrays 112. C++ prg.to enter a matrix and print its transpose 113. C++ prg.to multiply 2 matrices 114. C++ prg.to multiply 3 matrices 115. C++ prg.to print a pascal triangle with / without Array 116. String in C++ 117. C++ prg. to print String with spaces 118. C++ prg.to print string in reverse 119. C++ prg.to count A,E,I,O,U vowel 120. C++ prg.to enter a string and encode it 121. C++ prg.to enter a string with spaces and convert string in capital 122. C++ prg. to enter a character and print consonant or vowel 123. C++ prg. for string with spaces 124. C++ prg. for string sorting in ascending order 125. C++ prg. print & Arrange them in order of Title 126. C++ prg. to check whether a string is palindrome or not 127. Function( ) in C++ 128. What is Function Prototyping or Prototyes? 129. C++ prg. to print max number using function 130. C++ Function program examples 131. C++ Prg. to add 2 integers,floats and strings 132. C++ Examples of Function 133. C++ function to print Prime or Fibonicca number 134. C++ prg. to enter string, remove the space and print it 135. C++ prg. to enter number and check it is Palindrome or not 136. C++ prg. to convert positive integer to Binary number 137. C++ function examples 138. Recursive functions 139. General Purpose Programs 140. Loan & EMI calculation prg. 141. C++ Table print prg. 142. lower to UPPER case prg. 143. Age Distribution prg. 144. Bank note calculation prg. 145. Simple Depreciation prg. 146. Reducing bal.Depreciation prg. 147. C++ company manufactures prg. 148. Marksheet prg. 149. Income Tax prg. 150. Time calculator prg. 151. Distance converter prg. 152. Volume Air Calculation prg. 153. Time to fill Water Tank prg. 154. Salary Calculation prg. 155. Total Sale Calculation prg. 156. Male/Female pecentage prg. 157. Library Rent prg. 158. Office Expance prg. 159. Total Salary Calculation prg. 160. Profit or Loss prg. 161. Total Profit/Loss prg. 162. C++ prg. to Convert Amt to Word. 163. # define directive 164. The # undef DIRECTIVE 165. SCOPE RULES AND STORAGE CLASSES 166. Structures 167. C++ structure program to print result based on merit 168. C++ prg.for entry and search within structure 169. C++ prg.for sort the books by author and name of books 170. C++ prg.for Sort employee in descending order of their salary 171. C++ prg. to write structure student, sort it with marks and then name 172. Pointer to the structure 173. Unions 174. Pointers 175. Classes 176. C++ prg. insert,sort,delete,display and search an element in an array using class 177. C++ prg. to add,subtract,multiply and divide two numbers by using inline functions 178. C++ prg. to add,subtract,multiply and divide two numbers 179. C++ prg. to concatenate two string using forward declaration of member functions 180. C++ prg.find its mean,median,standard deviation. 181. Inline functions 182. Objects 183. Access specifiers 184. C++ prg. Example to pass object as parameter 185. C++ Returning object from a function 186. Friend functions 187. C++ Prg.to swap the values of two variables using friend functions 188. C++ Prg. use friend functions which accepts two arrays and merges them and sorts them 189. Friend class 190. Static members 191. Static functions 192. Constructor and destructor 193. Parameterized constructors 194. Dynamic allocation of memory 195. Copy Constructor 196. Inline constructors 197. Constructor overloading 198. Destructors 199. Polymorphism 200. Operator overloading 201. Inheritance 202. C++ Single inheritance 203. C++ Multiple inheritance 204. C++ Multilevel Inheritance 205. C++ Hierarchical inheritance 206. C++ Hybrid inheritance 207. C++ Multipath inheritance 208. Virtual functions 209. Concrete derived class 210. File Handling 211. Read contents from one file and paste it in other file 212. C++ Interview Part1 213. C++ Interview Part2 214. C++ Interview Part3 215. C++ Interview Part4 216. C++ Interview Part5 217. C++ Interview Part6 218. C++ Interview Part7 219. C++ Interview Part8 220. C++ Interview Part9 221. C++ Interview Part10 222. C++ Interview Part11 223. C++ Interview Part11
Pr.Pg Next Pg

Virtual functions tutorials

  • Virtual functions are also called as run time polymorphism.

  • A virtual member function preceded by the keyword virtual in the base class and redefined with the same name and same parameters in the derived class.

  • Here virtual means overridable.

  • The definition of the function in the derived class overrides the definition of the function of the base class.

  • When it is accessed normally, the virtual function behaves like any other member function.

  • If an object of the base class invokes the virtual function then the copy of the base class function is invoked and if an object of the derived class invokes the virtual function then the function which is defined in the derived class is invoked but if an object of the derived class is assigned to a base class pointer then the base class pointer invokes the overridden function in the derived class.

  • In C++ virtual functions are declared to specify late binding.

  • When a base class pointer points to the derived class objects, C++ determines which version of the function to be called, depending on the type of object pointed to by the pointer.

  • That means the runtime system automatically invokes the proper member function when it is overridden by the derived class.

 

Program c++ program example of virtual functions

Sam Madam

#include <iostream.h>

#include <conio.h>

class father

{

private:

char f_name[20];

int f_age;

public:

father(int a)

{

f_age=a;

}

void getdata()

{

cin.get(f_name,20);

}

virtual int getage()

{

return f_age;

}

};

 

class son:public father

{

private:

char s_name[20];

int s_age;

public:

son(int a,int b):father (a)

{

s_age=b;

}

void getdata()

{

cin.get(s_name,20);

}

int getage()

{

return s_age;

}

};

 

void main()

{

father x(45);

x.getdata();

son y(50,20);

y.getdata();

y.father::getdata();

father *p=&x;

cout<<endl<<"Fathers age : "<<p->getage();

p=&y;

cout<<endl<<"Son age : "<<p->getage();

// getch();

}

Output

Father's age : 50

Son's age : 20

 

  • In the above example for the two call statements p->getage(), two different functions are executed. When p contains address of father object x, function from father class is executed. When p contains address of son object y , function from son class is executed. When p contains address of son object y, function from son class is executed. Pointer p which is of base class father can point to objects of father and son class since getage() function is declared as virtual in base class and there exists overriding function in derived class.

 

Program to illustrate run time polymorphism i.e. virtual functions

Sam Madam

//WAP to illustrate run time polymorphism i.e. virtual functions

#include <iostream.h>

#include <conio.h>

#define pi 3.142

class oval

{

protected:

int a,b;

public:

void getdata (int x, int y)

{

a=x;

b=y;

}

virtual float area ()

{

return 0;

}

};

 

class circle: public oval

{

public:

float area ()

{

cout <<endl<<"Area of ellipse is : ";

return (pi*a*b);

}

};

 

class ellipse: public oval

{

public:

float area ()

{

cout <<endl<<"Area of circle having the radius same as that of the horizontal axis of the ellipse is : ";

return (pi*a*b);

}

};

 

void main ()

{

ellipse e1;

circle c1;

ellipse *e=&e1;

circle *c=&c1;

cout <<"Enter the length of major and minor axis of the ellipse : ";

float a1,b1;

cin>>a1>>b1;

e->getdata(a1,b1);

c->getdata(a1,a1);

cout <<e->area();

cout <<c->area();

getch();

}

 

 

Output

Enter the length of major and minor axis of the ellipse : 7 1

 

Area of circle having the radius same as that of the horizontal axis of the elli

pse is : 21.994

Area of ellipse is : 153.958

 

RULES FOR VIRTUAL FUNCTIONS

  • The virtual functions should not be static and must be member of a class.

  • A virtual function may be declared as friend for another class. Object pointer can access virtual functions.

  • Constructors cannot be declared as virtual, but destructors can be declared as virtual.

  • The virtual function must be defined in public section of the class. It is also possible to define the virtual function outside the class. In such a case, the declaration is done inside the class and definition is done outside the class. The virtual keyword is used in the declaration and not in function declarator.

  • It is also possible to return a value from virtual function like other functions.

  • The prototype of virtual functions in base and derived classes should be exactly the same. In case of mismatch, the compiler neglects the virtual function mechanism and treats them as overloaded functions.

 

Virtual function v/s Late binding

  • To accomplish late binding, the compiler creates a table called VTABLE for each class that contains virtual functions in the table.

  • If the virtual function of the base class is not defined in the derived class, the compiler uses the address of the base class version in the derived class.

  • When an object of the base class or the derived class is created, the compiler secretly places a pointer called the vpointer, which points to the vtable.

  • When a call is made through a base class pointer, the compiler quietly inserts the code to fetch the vpointer and looks up the function address in the vtable thus, calling the right function and causing the late binding to take place.

 

Difference between overloading and overriding

  • Overloading occurs when two or more functions are in the same scope and have the same name but different parameters.

  • Overriding occurs when a class and one of its derived classes both define a member function with the same name and same set of parameters, and the member function is declared to be virtual in the base class.

 

Pure virtual function

  • We can abstract common features of all the derived classes into base class.

  • In such case objects of base class are never created. Such a class is called abstract class.

  • If overriding functions are used in derived classes, then there must be function in base class.

  • The function in base class may not perform any activity.

  • When a virtual function in base class is not performing any activity and simply used for overriding concept, then the function from the base class is called pure virtual function. To make virtual function pure virtual function, remove body of the function, remove body of the function and assign 0 to declaration.

  • Once base class function is pure virtual, objects of base class cannot be created and base class is called as abstract class.

 

Uses of pure virtual function

  • When a base class is not able to define itís virtual function but a derive class can give definition.

  • When a base class wants to ensure that derived class overrides a virtual function. It constitutes a foundation for the derived class.

 

Abstract class

  • A class that contains at least one pure virtual function is said to be abstract class. An object of an abstract class cannot be created, because it contains one or more pure virtual function for which there is no definition.

  • Abstract class constitutes a foundation for the derived class. Hence they are called as foundation class.

  • A reference or a pointer can be created to an abstract class. This allows abstract classes to support run-time polymorphism, which relies upon base class pointers.

  • All derived class of the abstract class must override the pure virtual function of it. If not, the derived class too becomes an abstract class.

  • Through abstract class, we can create generic, extensible class libraries that can be used by the programmers. The other programmer takes that concept and put their own implementation.

 

Pr.Pg border                                              Next Pg