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

Operator overloading tutorials

  • Operators in C++ operate on simple types such as char, int, float, double etc.

  • However we can allow the same operators to operate on user defined types such as objects which is called as operator overloading.

  • The difference in the operation is identified by the data types of operands.

  • If operands are of simple types normal operation is carried out where as if operands are objects then overloaded operation is carried out.

  • When operator is overloaded, there exists operator function within class.

  • If operator is encountered with the object directly, operator function from the class is executed.

  • The operator function can be returning or non-returning.

  • By using operator overloading concept we can completely redefine the language.

  • We can totally avoid calling member function by using (.) operator.

  • With operator overloading, the statement m3=m1+m2; looks more readable.

  • If m1 and m2 are objects operator function from the class is executed.

  • Separate call statement is not required.

 

Rules for operator overloading

  • Only existing operators can be overloaded.

  • The overloaded operator must have at least one user-defined operand.

  • It is not recommended to change the basic meaning of an operator.

  • Operator function cannot have all the default arguments.

  • The following operators cannot be overloaded

 

. (Dot)

Dot member access operator

::

Scope resolution operator

?:

Ternary operator, conditional operator

.*

Pointer to member operator

sizeof(size)

 

Typeid

 

  • Friend functions cannot be used to overload certain operators like ( ),[ ] and ->, only a non-static member function can overload these operators. This ensures that their first operand will be values (left handed values: where the results are stored).

  • The precedence of the operator cannot be altered. For example, * operator always has the higher precedence over the + operator. There is no way to change the precedence.

  • The user cannot change the meaning of an expression unless the expression contains an object of the user defined type.

  • All the binary arithmetic overloaded operator function explicitly returns a value.

  • It is not possible to define an operator function that operates exclusively on pointers. This affirms that C++ is extensible but not mutable, i.e. cannot be changed.

 

Format of operator function

 

return type operator #(arg-list)

{

//operator

}

 

Return-type, either primary or secondary data type depending on the need.

Operator:keyword

# :placeholder for any operator

Arg-list : formal parameters

 

Unary Operators

  • A unary operator acts on a single operand (e.g. -,++).

  • It can be overloaded either through a non-static member function or a non-member function (friend function)

  • When a unary operator is overloaded through a member function then the operator function takes no parameter. The statement ++ x, will be interpreted as x.operator++()

  • If it is overloaded through a friend function, it takes one parameter of type reference to the object. Since a friend function is not a member of the class, it does not have a this pointer. Therefore the object is passed as a parameter and the operator function receives it as a reference, in order to reflect the changes on the operand. Hence the statement x++; will be interpreted as operator++(x);

  • When a unary operator is overloaded, the operator function may not return a value. If it returns, the value can be either a primary or secondary data type, depending on the need.

 

Program to illustrate unary operator overloading

Sam Madam

//WAP to illustrate unary operator overloading

#include <iostream.h>

#include <conio.h>

class data

{

private:

intm,n;

public:

data()

{}

data (inta,int b)

{

m=a;

n=b;

}

void operator++()

{

m++;

n++;

}

 

data operator++(int)

{

data z;

z.m=m++;

z.n=n++;

return z;

}

 

void operator--()

{

m--;

n--;

}

 

data operator-()

{

m=-m;

n=-n;

return *this;

}

 

void display()

{

cout<<endl<<m<<'\t'<<n;

}

};

 

void main()

{

data x(50,-100),y(100,-50);

++x;

data z=x++;

--y;

z--;

x=-x;

y=-y;

x.display();

y.display();

z.display();

getch();

}

Output

-52 98

-99 51

50 -100

 

  • In the above example for y and y--, same operator-() function is executed. Thus when there is only one function for increment or decrement operator, then same function is executed for prefix and postfix.

  • We can differentiate prefix and postfix operations by including one or more operator function for same operator. For this operator function int is written in(), where int is not data type of argument. It is used to differentiate prefix and postfix operations. For prefix operation function with no argument is executed and for postfix operation function with int is executed.

  • The operator function can be void as well as returning. If it is returning object, then expression with the operator should be RHS of assignment. With ++x members of x are incremented and with y=x++; , x is assigned to y and then members of calling object x are incremented. The existing values are stored in z first which is returned then.

  • Unary - is also overloaded as it appears with objects. Its members are changed in sign and the same object is returned because expression with appears as RHS of assignment.

 

Binary Operator Overloading

  • A binary operator is an operator that requires two operands.

  • A binary operator can be overloaded by either a non-static member function taking one argument or a non-member function taking two arguments.

  • If the operator function is a member function, then the first operand must be a user defined data type whereas the second operand can be either primary or user defined data type.

 

Program to illustrate binary operator overloading

Sam Madam

//WAP to illustrate binary operator overloading

#include<iostream.h>

#include<conio.h>

class complex

{

inta,b;

public:

void getvalue()

{

cout<<"Enter the value of Complex Numbers a,b:";

cin>>a>>b;

}

complex operator+(complex ob)

{

complex t;

t.a=a+ob.a;

t.b=b+ob.b;

return(t);

}

complex operator-(complex ob)

{

complex t;

t.a=a-ob.a;

t.b=b-ob.b;

return(t);

}

void display()

{

cout<<a<<"+"<<b<<"i"<<"\n";

}

};

 

void main()

{

clrscr();

complex obj1,obj2,result,result1;

 

obj1.getvalue();

obj2.getvalue();

 

result = obj1+obj2;

result1=obj1-obj2;

cout<<"Input Values:\n";

obj1.display();

obj2.display();

cout<<"Result:";

result.display();

result1.display();

getch();

}

Output

Enter the value of Complex Numbers a, b

4 5

Enter the value of Complex Numbers a, b

2 2

Input Values

4 + 5i

2 + 2i

Result

6 + 7i

2 + 3i

  • In the above example, the expression result=obj1+obj2; the first and second operands are of the same type the operator function is called for obj1 and obj2 sent as argument to function. The function should be returning and return type is complex number.

 

Assignment Operators

  • Assignment (=) and the composite assignment (e.g +=) operator function does not return a value. The changes are made in the left hand operand (calling object, if member function ).

d1+=d2;

 

  • If the operator function is friend function, then the first parameter must be a reference to the object.

distance operator +=(distance &d1,distance d2)

{

}

calling :d1+=d2;

 

  • When an object is assigned to another object in the line of declaration, the copy constructor is called

distance d2=d1;

 

  • But if an object is assigned to another object on the next line of declaration, then the equal to operator is called.

distance d2;

d2=d1;

 

  • When the operator =() function is explicitly defined in the class then the changes will be made to the target object (d2) according to the definition of the function. If not, the default equal to operator function is called. The operator function copies the content of the source object into the target object.

 

Pr.Pg border                                              Next Pg