#Include : Using Namespace
#Include : Using Namespace
#Include : Using Namespace
*/
#include <iostream>
using namespace std;
class Hello
{
public:
void sayHello()
{
cout << "Hello World" << endl;
}
};
int main()
{
Hello h;
h.sayHello();
return 0;
}
Output
Hello World
1. #include <iostream>
2.
3. using namespace std;
4.
5. class complex
6. {
7. public :
8. int real, img;
9. };
10.
11. int main()
12. {
13. complex a, b, c;
14.
15. cout << "Enter a and b where a + ib is the
first complex number.";
16. cout << "\na = ";
17. cin >> a.real;
18. cout << "b = ";
19. cin >> a.img;
20. cout << "Enter c and d where c + id is the
second complex number.";
21. cout << "\nc = ";
22. cin >> b.real;
23. cout << "d = ";
24. cin >> b.img;
25.
26. c.real = a.real + b.real;
27. c.img = a.img + b.img;
28.
29. if (c.img >= 0)
30. cout << "Sum of two complex numbers =
" << c.real << " + " << c.img << "i";
31. else
32. cout << "Sum of two complex numbers =
" << c.real << " " << c.img << "i";
33.
34. return 0;
35. }
Function Overloading
void display(int);
void display(float);
void display(int, float);
int main() {
int a = 5;
float b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}
Output
Integer number: 5
Here, the display() function is called three times with different type or number of arguments.
The return type of all these functions are same but it's not necessary.
#include <iostream>
using namespace std;
int absolute(int);
float absolute(float);
int main() {
int a = -5;
float b = 5.5;
cout << "Absolute value of " << a << " = " << absolute(a) << endl;
cout << "Absolute value of " << b << " = " << absolute(b);
return 0;
}
Output
Absolute value of -5 = 5
Operator Overloading
class Test
{
private:
int count;
public:
Test(): count(5){}
int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}
Output
Count: 6
This function is called when ++ operator operates on the object of Test class (object t in this case).
#include <iostream>
public:
int x;
void getdata()
};
private:
int y;
public:
void readdata()
void product()
};
int main()
a.getdata();
a.readdata();
a.product();
return 0;
} //end of program
Output
Enter the value of x = 3
Product = 12
Explanation
In this program class derive is publicly derived from the base class base. So the class derive inherits
all the protected and public members of base class base i.e the protected and the public members of
base class are accessible from class derive.
Multilevel Inheritance
// inheritance.cpp
#include <iostream>
public:
int x;
void getdata()
};
public:
int y;
void readdata()
};
private:
int z;
public:
void indata()
void product()
};
int main()
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
} //end of program
Output
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
Multiple Inheritance
// multiple inheritance.cpp
#include
class A
public:
int x;
void getx()
};
class B
public:
int y;
void gety()
};
public:
void sum()
};
int main()
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
} //end of program
Output
enter value of x: 5
enter value of y: 4
Sum = 9
Explanation
In the above program, there are two base class A and B from which class C is inherited. Therefore,
derived class C inherits all the public members of A and B and retains their visibility. Here, we have
created the object obj1 of derived class C.
#include<iostream.h>
#include<conio.h>
class CalculateArea
{
public:
void Area(int r) //Overloaded Function 1
{
cout<<"\n\tArea of Circle is : "<<3.14*r*r;
}
};
void main()
{
CalculateArea C;
C.Area(5); //Statement 1
C.Area(5,3); //Statement 2
C.Area(7,2.1f); //Statement 3
C.Area(4.7f,2); //Statement 4
}
Output :
Virtual Function
#include<iostream>
using namespace std;
class base
{
public:
virtual void print ()
{ cout<< "print base class" <<endl; }
void show ()
{ cout<< "show base class" <<endl; }
};
void show ()
{ cout<< "show derived class" <<endl; }
};
int main()
{
base *bptr;
derived d;
bptr = &d;
#include<iostream>
using namespace std;
class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};
int main(void)
{
Derived d;
d.fun();
return 0;
}
Output:
fun() called
struct Distance{
int feet;
float inch;
}d1 , d2, sum;
int main()
{
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;
sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;
cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << "
inches";
return 0;
}
Output
Enter feet: 6
Enter feet: 5
In this program, a structure Distance containing two data members (inch and feet) is declared to
store the distance in inch-feet system.