#Include : Using Namespace

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

/* C++ program to create a simple class and object.

*/

#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

Example 1: Function Overloading


#include <iostream>
using namespace std;

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;
}

void display(int var) {


cout << "Integer number: " << var << endl;
}

void display(float var) {


cout << "Float number: " << var << endl;
}

void display(int var1, float var2) {


cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}

Output

Integer number: 5

Float number: 5.5

Integer number: 5 and float number: 5.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.

Example 2: Function Overloading


// Program to compute absolute value
// Works both for integer and float

#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;
}

int absolute(int var) {


if (var < 0)
var = -var;
return var;
}

float absolute(float var){


if (var < 0.0)
var = -var;
return var;
}

Output

Absolute value of -5 = 5

Absolute value of 5.5 = 5.5

In the above example, two functions absolute() are overloaded.

Operator Overloading

Example: Operator overloading in C++ Programming


#include <iostream>
using namespace std;

class Test
{
private:
int count;

public:
Test(): count(5){}

void operator ++()


{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};

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).

In the program,void operator ++ () operator function is defined (inside Test class).

This function increments the value of count by 1 for t object.

Single level inheritance

C++ Single Inheritance Example


// inheritance.cpp

#include <iostream>

using namespace std;

class base //single base class


{

public:

int x;

void getdata()

cout << "Enter the value of x = "; cin >> x;

};

class derive : public base //single derived class

private:

int y;

public:

void readdata()

cout << "Enter the value of y = "; cin >> y;

void product()

cout << "Product = " << x * y;

};
int main()

derive a; //object of derived class

a.getdata();

a.readdata();

a.product();

return 0;

} //end of program

Output
Enter the value of x = 3

Enter the value of y = 4

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

C++ Multilevel Inheritance Example


Here is a simple example to illustrate the concept of c++ multilevel inheritance.

// inheritance.cpp

#include <iostream>

using namespace std;


class base //single base class

public:

int x;

void getdata()

cout << "Enter value of x= "; cin >> x;

};

class derive1 : public base // derived class from base class

public:

int y;

void readdata()

cout << "\nEnter value of y= "; cin >> y;

};

class derive2 : public derive1 // derived from class derive1

private:

int z;

public:
void indata()

cout << "\nEnter value of z= "; cin >> z;

void product()

cout << "\nProduct= " << x * y * z;

};

int main()

derive2 a; //object of derived class

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

C++ Multiple Inheritance Example


Here is a simple example illustrating the concept of C++ multiple inheritance.

// multiple inheritance.cpp

#include

using namespace std;

class A

public:

int x;

void getx()

cout << "enter value of x: "; cin >> x;

};

class B

public:

int y;
void gety()

cout << "enter value of y: "; cin >> y;

};

class C : public A, public B //C is derived from class A and class B

public:

void sum()

cout << "Sum = " << x + y;

};

int main()

C obj1; //object of derived class C

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 Area(int l,int b) //Overloaded Function 2


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

void Area(float l,int b) //Overloaded Function 3


{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}
void Area(int l,float b) //Overloaded Function 4
{
cout<<"\n\tArea of Rectangle is : "<<l*b;
}

};

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 :

Area of Circle is : 78.5


Area of Rectangle is : 15
Area of Rectangle is : 14.7
Area of Rectangle is : 29.4

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; }
};

class derived:public base


{
public:
void print ()
{ cout<< "print derived class" <<endl; }

void show ()
{ cout<< "show derived class" <<endl; }
};

int main()
{
base *bptr;
derived d;
bptr = &d;

//virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at compile time


bptr->show();
}
Output:
print derived class
show base class

Pure Virtual Function

#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class inherits from Base and implements fun()


class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};

int main(void)
{
Derived d;
d.fun();
return 0;
}
Output:
fun() called

Example: Add Distances Using Structures


#include <iostream>
using namespace std;

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;

cout << "\nEnter information for 2nd distance" << endl;


cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;

sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;

// changing to feet if inch is greater than 12


if(sum.inch > 12)
{
++ sum.feet;
sum.inch -= 12;
}

cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << "
inches";
return 0;
}

Output

Enter 1st distance,

Enter feet: 6

Enter inch: 3.4

Enter information for 2nd distance

Enter feet: 5

Enter inch: 10.2

Sum of distances = 12 feet 1.6 inches

In this program, a structure Distance containing two data members (inch and feet) is declared to
store the distance in inch-feet system.

You might also like