0% found this document useful (0 votes)
288 views47 pages

CS112

1. The document contains 17 code examples demonstrating various C++ programming concepts like arrays, if/else statements, loops, functions, structures, classes, and pointers. The examples range from basic programs to calculate sums and factorials to more advanced concepts like structures, classes and object-oriented programming. 2. Key concepts demonstrated include arrays, if/else and switch statements, for, while, do-while loops, break and continue statements, functions, structures, passing structures to functions, pointers to structures, enumeration, classes, objects, and encapsulation. 3. The examples provide a good overview of fundamental and intermediate C++ programming techniques for learning and demonstrating understanding of the language.

Uploaded by

Chikku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
288 views47 pages

CS112

1. The document contains 17 code examples demonstrating various C++ programming concepts like arrays, if/else statements, loops, functions, structures, classes, and pointers. The examples range from basic programs to calculate sums and factorials to more advanced concepts like structures, classes and object-oriented programming. 2. Key concepts demonstrated include arrays, if/else and switch statements, for, while, do-while loops, break and continue statements, functions, structures, passing structures to functions, pointers to structures, enumeration, classes, objects, and encapsulation. 3. The examples provide a good overview of fundamental and intermediate C++ programming techniques for learning and demonstrating understanding of the language.

Uploaded by

Chikku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 47

1. C++ program to store and calculate the sum of 5 numbers entered by the user using arrays.

#include <iostream.h>

int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";

for (int i = 0; i < 5; ++i)


{
cin >> numbers[i];
sum += numbers[i];
}

cout << "Sum = " << sum << endl;

return 0;
}

Output
Enter 5 numbers: 3
4
5
4
2
Sum = 18

2. C++ if Statement
// Program to print positive number entered by the user
// If user enters negative number, it is skipped

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

// checks if the number is positive


if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
1|Page
}

cout << "This statement is always executed.";


return 0;

Output 1
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
Output 2
Enter a number: -5
This statement is always executed.

3. C++ if...else Statement


// Program to check whether an integer is positive or negative
// This program considers 0 as positive number

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

if ( number >= 0)
{
cout << "You entered a positive integer: " << number << endl;
}

else
{
cout << "You entered a negative integer: " << number << endl;
}

cout << "This line is always printed.";


return 0;
}

Output
Enter an integer: -4
You entered a negative integer: -4.
This line is always printed.

2|Page
4. C++ Nested if...else
// Program to check whether an integer is positive, negative or zero

#include <iostream>
using namespace std;

int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;

if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0)
{
cout<<"You entered a negative integer: " << number << endl;
}
else
{
cout << "You entered 0." << endl;
}

cout << "This line is always printed.";


return 0;
}

Output
Enter an integer: 0
You entered 0.
This line is always printed.

5. C++ for Loop


// C++ Program to find factorial of a number
// Factorial on n = 1*2*3*...*n

#include <iostream>
using namespace std;

int main()
{
int i, n, factorial = 1;

3|Page
cout << "Enter a positive integer: ";
cin >> n;

for (i = 1; i <= n; ++i) {


factorial *= i; // factorial = factorial * i;
}

cout<< "Factorial of "<<n<<" = "<<factorial;


return 0;
}

Output
Enter a positive integer: 5
Factorial of 5 = 120

6. C++ while Loop


//C++ Program to compute factorial of a number
// Factorial of n = 1*2*3...*n

#include <iostream>
using namespace std;

int main()
{
int number, i = 1, factorial = 1;

cout << "Enter a positive integer: ";


cin >> number;

while ( i <= number) {


factorial *= i; //factorial = factorial * i;
++i;
}

cout<<"Factorial of "<< number <<" = "<< factorial;


return 0;
}

Output
Enter a positive integer: 4
Factorial of 4 = 24

7. C++ do...while Loop


// C++ program to add numbers until user enters 0

4|Page
#include <iostream>
using namespace std;

int main()
{
float number, sum = 0.0;

do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);

cout<<"Total sum = "<<sum;

return 0;
}

Output
Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: -4
Enter a number: 2
Enter a number: 4.4
Enter a number: 2
Enter a number: 0

8. C++ break

C++ program to add all number entered by user until user enters 0.
// C++ Program to demonstrate working of break statement

#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;

// test expression is always true


while (true)
{
cout << "Enter a number: ";
cin >> number;

if (number != 0.0)
{
sum += number;

5|Page
}
else
{
// terminates the loop if number equals 0.0
break;
}

}
cout << "Sum = " << sum;

return 0;
}

Output
Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6

9. C++ continue

C++ program to display integer from 1 to 10 except 6 and 9.

include <iostream>
using namespace std;

int main()
{
for (int i = 1; i <= 10; ++i)
{
if ( i == 6 || i == 9)
{
continue;
}
cout << i << "\t";
}
return #0;
}

Output
1 2 3 4 5 7 8 10

6|Page
10. C++ switch Statement
// Program to built a simple calculator using switch Statement

#include <iostream>
using namespace std;

int main()
{
char o;
float num1, num2;

cout << "Enter an operator (+, -, *, /): ";


cin >> o;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch (o)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1/num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! operator is not correct";
break;
}

return 0;
}

Output
Enter an operator (+, -, *, /): +
-
Enter two operands: 2.3
4.5
2.3 - 4.5 = -2.2

7|Page
11. Goto Statement
// This program calculates the average of numbers entered by user.
// If user enters negative number, it ignores the number and
// calculates the average of number entered before it.

#include <iostream>
using namespace std;

int main()
{
float num, average, sum = 0.0;
int i, n;

cout << "Maximum number of inputs: ";


cin >> n;

for(i = 1; i <= n; ++i)


{
cout << "Enter n" << i << ": ";
cin >> num;

if(num < 0.0)


{
// Control of the program move to jump:
goto jump;
}
sum += num;
}

jump:
average = sum / (i - 1);
cout << "\nAverage = " << average;
return 0;
}

Output
Maximum number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6
Average = 3.95

12. C++ Program to assign data to members of a structure variable and display it.
#include <iostream.h>
8|Page
struct Person
{
char name[50];
int age;
float salary;
};

int main()
{
Person p1;

cout << "Enter Full name: ";


cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;

cout << "\nDisplaying Information." << endl;


cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary;

return 0;
}

Output
Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4

13. Returning structure from function in C++


#include <iostream.h>

struct Person {
char name[50];
int age;
float salary;
};

9|Page
Person getData(Person);
void displayData(Person);

int main()
{

Person p;

p = getData(p);
displayData(p);

return 0;
}

Person getData(Person p) {

cout << "Enter Full name: ";


cin.get(p.name, 50);

cout << "Enter age: ";


cin >> p.age;

cout << "Enter salary: ";


cin >> p.salary;

return p;
}

void displayData(Person p)
{
cout << "\nDisplaying Information." << endl;
cout << "Name: " << p.name << endl;
cout <<"Age: " << p.age << endl;
cout << "Salary: " << p.salary;
}

Output
Enter Full name: Magdalena Dankova
Enter age: 27
Enter salary: 1024.4
Displaying Information.
Name: Magdalena Dankova
Age: 27
Salary: 1024.4

14. Pointers to Structure

10 | P a g e
#include <iostream.h>

struct Distance
{
int feet;
float inch;
};

int main()
{
Distance *ptr, d;

ptr = &d;

cout << "Enter feet: ";


cin >> (*ptr).feet;
cout << "Enter inch: ";
cin >> (*ptr).inch;

cout << "Displaying information." << endl;


cout << "Distance = " << (*ptr).feet << " feet " << (*ptr).inch << "
inches";

return 0;
}

Output
Enter feet: 4
Enter inch: 3.5
Displaying information.
Distance = 4 feet 3.5 inches

15. Enumeration Type


#include <iostream.h>

enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

int main()
{
week today;
today = Wednesday;
cout << "Day " << today+1;
return 0;
}

Output
11 | P a g e
Day 4

16. Program to illustrate the working of objects and class in C++ Programming
#include <iostream.h>

class Test
{
private:
int data1;
float data2;

public:

void insertIntegerData(int d)
{
data1 = d;
cout << "Number: " << data1;
}

float insertFloatData()
{
cout << "\nEnter data: ";
cin >> data2;
return data2;
}
};

int main()
{
Test o1, o2;
float secondDataOfObject2;

o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();

cout << "You entered " << secondDataOfObject2;


return 0;
}

Output
Number: 12
Enter data: 23.3
You entered 23.3

17.Program to explain Simple Class and Object

12 | P a g e
#include <iostream.h>
#include <conio.h>
#define PI 3.14

class Circle
{
private:
float radius;
public:
void getRadius()
{
cout << "Enter Radius ::: ";
cin >> radius;
}

void area()
{
float ar;
ar = PI * radius * radius;
cout << "\n Area of Circle : " << ar;
}

void showRadius()
{
cout << "\n Radius : " << radius;
}
};

void main()
{
clrscr();
Circle c1;

c1.getRadius();
c1.showRadius();
c1.area();

getch();
}

Output:

Enter Radius ::: 10

Radius : 10
Area of Circle : 314

18. C++ program to show class with argument


13 | P a g e
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>

class stringfun
{
char name[20];
public:
void concatString(char a[],char b[])
{
strcat(a,b);
strcpy(name,a);
}
void display()
{
cout<<"\nName : "<<name;
}
};

void main()
{
char str1[10],str2[10];
clrscr();

stringfun sf;

cout<<"Enter your name:";


gets(str1);
cout<<"Enter sir name:";
gets(str2);
sf.concatString(str1,str2);
sf.display();
getch();
}

Output:
Enter your name:Nils
Enter sir name:Patel

Name : NilsPatel

19. Calculate the area of a rectangle and display it. (Constructor in C++)
#include <iostream.h>
14 | P a g e
class Area
{
private:
int length;
int breadth;

public:
// Constructor
Area(): length(5), breadth(2){ }

void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}

int AreaCalculation() { return (length * breadth); }

void DisplayArea(int temp)


{
cout << "Area: " << temp;
}
};

int main()
{
Area A1, A2;
int temp;

A1.GetLength();
temp = A1.AreaCalculation();
A1.DisplayArea(temp);

cout << endl << "Default Area when value is not taken from user" << endl;

temp = A2.AreaCalculation();
A2.DisplayArea(temp);

return 0;
}

20. C++ program to use Parameterized Constructor


// and Constructor Overloading

#include <iostream.h>
#include <conio.h>
#define PI 3.14
15 | P a g e
class Circle
{
float radius;
public:
Circle();
Circle(float);
void getRadius();
float area();
void showRadius();
};

Circle :: Circle()
{
radius = 10;
}

Circle :: Circle( float r )


{
radius = r;
}

void Circle :: getRadius()


{
cout << "Enter Radius ::: ";
cin >> radius;
}

float Circle :: area()


{
float ar;
ar = PI * radius * radius;
return ar;
}

void Circle :: showRadius()


{
cout << "\n Radius : " << radius;
}

void main()
{
clrscr();
Circle c1(10);

c1.showRadius();
float a = c1.area();
cout << "\n Area of Circle : " << a;

16 | P a g e
getch();
}

Output:

Radius : 10
Area of Circle : 314

21. Constructor overloading.


// Source Code to demonstrate the working of overloaded constructors

#include <iostream>
using namespace std;

class Area
{
private:
int length;
int breadth;

public:
// Constructor with no arguments
Area(): length(5), breadth(2) { }

// Constructor with two arguments


Area(int l, int b): length(l), breadth(b){ }

void GetLength()
{
cout << "Enter length and breadth respectively: ";
cin >> length >> breadth;
}

int AreaCalculation() { return length * breadth; }

void DisplayArea(int temp)


{
cout << "Area: " << temp << endl;
}
};

int main()
{
Area A1, A2(2, 1);
int temp;

cout << "Default Area when no argument is passed." << endl;


temp = A1.AreaCalculation();
17 | P a g e
A1.DisplayArea(temp);

cout << "Area when (2,1) is passed as argument." << endl;


temp = A2.AreaCalculation();
A2.DisplayArea(temp);

return 0;
}

Output
Default Area when no argument is passed.
Area: 10
Area when (2,1) is passed as argument.
Area: 2

22. C++ program to show the use of destructor.


#include<conio.h>
#include<iostream.h>

class myclass
{
private:
int t;
public:
myclass()
{
cout<<"This is A Constructor\n";
}
~myclass()
{
cout<<"This is A Destructor\n";
}
void set(int d)
{
t=d;
}
void show()
{
cout<<t<<endl;
}
};

void main()
{
clrscr();
myclass m1,m2;
m1.set(2);

18 | P a g e
m2.set(6);
m1.show();
m2.show();
cout<<"Last Statement\n";
getch();
}

Output:

This is A Constructor
This is A Constructor
2
6
Last Statement
This is A Destructor
This is A Destructor

23. C++ program to show simple Member Function


#include<iostream.h>
#include<conio.h>
class member
{
public:
void inside()
{
cout<<"This is Inside Member Function"<<endl;
}
void outside();
};

void member::outside()
{
cout<<"This is Outside Member Function";
}

void main()
{
clrscr();
member m;
m.inside();
m.outside();
getch();
}

Output:
This is Inside Member Function
19 | P a g e
This is Outside Member Function

24. C++ program to show static data member and Function


#include<iostream.h>
#include<conio.h>

class stat
{
int code;
static int count;

public:
stat()
{
code=++count;
}
void showcode()
{
cout<<"\n\tObject number is :"<<code;
}
static void showcount()
{
cout<<"\n\tCount Objects :"<<count;
}
};

int stat::count;

void main()
{
clrscr();
stat obj1,obj2;

obj1.showcount();
obj1.showcode();
obj2.showcount();
obj2.showcode();
getch();
}

Output:
Count Objects :2
Object number is :1
Count Objects :2
Object number is :2

20 | P a g e
25. C++ program to show inline Function example
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
cout<<"\n\nCube value is
:"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}

Output:

Enter two values:10 20

Multiplication value is:200


Cube value is :1000 8000

26. C++ program to add two complex numbers by passing objects to a function.
#include <iostream.h>

class Complex
{
private:
int real;
21 | P a g e
int imag;
public:
Complex(): real(0), imag(0) { }
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
void addComplexNumbers(Complex comp1, Complex comp2)
{
// real represents the real data of object c3 because this function
is called using code c3.add(c1,c2);
real=comp1.real+comp2.real;

// imag represents the imag data of object c3 because this


function is called using code c3.add(c1,c2);
imag=comp1.imag+comp2.imag;
}

void displaySum()
{
cout << "Sum = " << real<< "+" << imag << "i";
}
};
int main()
{
Complex c1,c2,c3;

c1.readData();
c2.readData();

c3.addComplexNumbers(c1, c2);
c3.displaySum();

return 0;
}

Output
Enter real and imaginary number respectively:
2
4
Enter real and imaginary number respectively:
-3
4
Sum = -1+8i

22 | P a g e
27. Pass and Return Object from the Function

In this program, the sum of complex numbers (object) is returned to the main() function and displayed.

#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int imag;
public:
Complex(): real(0), imag(0) { }
void readData()
{
cout << "Enter real and imaginary number respectively:"<<endl;
cin >> real >> imag;
}
Complex addComplexNumbers(Complex comp2)
{
Complex temp;

// real represents the real data of object c3 because this


function is called using code c3.add(c1,c2);
temp.real = real+comp2.real;

// imag represents the imag data of object c3 because this


function is called using code c3.add(c1,c2);
temp.imag = imag+comp2.imag;
return temp;
}
void displayData()
{
cout << "Sum = " << real << "+" << imag << "i";
}
};

int main()
{
Complex c1, c2, c3;

c1.readData();
c2.readData();

c3 = c1.addComplexNumbers(c2);

c3.displayData();

return 0;
23 | P a g e
}

28. Operator overloading in C++ Programming


#include <iostream.h>

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

29. C++ Program to demonstrate the working of pointer.


#include <iostream.h>

int main()
{
int *pc, c;

c = 5;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;

24 | P a g e
pc = &c; // Pointer pc holds the memory address of variable c
cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl
<< endl;

c = 11; // The content inside memory address &c is changed from 5 to


11.
cout << "Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl
<< endl;

*pc = 2;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;

return 0;
}

Output
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5
Address that pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 5
Address pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 11
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 2

30. Program to use this Pointer


#include <iostream.h>
#include <conio.h>
#define PI 3.14

class Circle
{
float radius;
public:
void setRadius( float radius );
float getRadius();
float area();
};

void Circle :: setRadius( float radius )


{
this -> radius = radius;
25 | P a g e
}

float Circle :: getRadius()


{
return this -> radius;
}

float Circle :: area()


{
return PI * radius * radius;
}

void main()
{
clrscr();
Circle c1;

c1.setRadius(10);
float a = c1.area();
cout << "\n Radius : " << c1.getRadius();
cout << "\n Area of Circle : " << a;

getch();
}

Output:

Radius : 10
Area of Circle : 314

31. C++ Program to display address of elements of an array using both array and pointers
#include <iostream.h>

int main()
{
float arr[5];
float *ptr;

cout << "Displaying address using arrays: " << endl;


for (int i = 0; i < 5; ++i)
{
cout << "&arr[" << i << "] = " << &arr[i] << endl;
}

// ptr = &arr[0]
ptr = arr;

26 | P a g e
cout<<"\nDisplaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}

return 0;
}

Output
Displaying address using arrays:
&arr[0] = 0x7fff5fbff880
&arr[1] = 0x7fff5fbff884
&arr[2] = 0x7fff5fbff888
&arr[3] = 0x7fff5fbff88c
&arr[4] = 0x7fff5fbff890
Displaying address using pointers:
ptr + 0 = 0x7fff5fbff880
ptr + 1 = 0x7fff5fbff884
ptr + 2 = 0x7fff5fbff888
ptr + 3 = 0x7fff5fbff88c
ptr + 4 = 0x7fff5fbff890

32. C++ Program to insert and display data entered by using pointer notation.
#include <iostream.h>

int main() {
float arr[5];

// Inserting data using pointer notation


cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
cin >> *(arr + i) ;
}

// Displaying data using pointer notation


cout << "Displaying data: " << endl;
for (int i = 0; i < 5; ++i) {
cout << *(arr + i) << endl ;
}

return 0;
}

Output
Enter 5 numbers: 2.5
3.5
4.5
27 | P a g e
5
2
Displaying data:
2.5
3.5
4.5
5
2

33. Passing by reference without pointers.


#include <iostream.h>

// Function prototype
void swap(int&, int&);

int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

swap(a, b);

cout << "\nAfter swapping" << endl;


cout << "a = " << a << endl;
cout << "b = " << b << endl;

return 0;
}

void swap(int& n1, int& n2) {


int temp;
temp = n1;
n1 = n2;
n2 = temp;
}

Output
Before swapping
a = 1
b = 2
After swapping
a = 2
b = 1

28 | P a g e
34. Passing by reference using pointers.
#include <iostream.h>

// Function prototype
void swap(int*, int*);

int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

swap(&a, &b);

cout << "\nAfter swapping" << endl;


cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}

void swap(int* n1, int* n2) {


int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}

Output
Before swapping
a = 1
b = 2
After swapping
a = 2
b = 1

35. Inheritance in C++ Programming.


//Create game characters using the concept of inheritance.

#include <iostream.h>

class Person
{
public:
string profession;
int age;

29 | P a g e
Person(): profession("unemployed"), age(16) { }
void display()
{
cout << "My profession is: " << profession << endl;
cout << "My age is: " << age << endl;
walk();
talk();
}
void walk() { cout << "I can walk." << endl; }
void talk() { cout << "I can talk." << endl; }
};

// MathsTeacher class is derived from base class Person.


class MathsTeacher : public Person
{
public:
void teachMaths() { cout << "I can teach Maths." << endl; }
};

// Footballer class is derived from base class Person.


class Footballer : public Person
{
public:
void playFootball() { cout << "I can play Football." << endl; }
};

int main()
{
MathsTeacher teacher;
teacher.profession = "Teacher";
teacher.age = 23;
teacher.display();
teacher.teachMaths();

Footballer footballer;
footballer.profession = "Footballer";
footballer.age = 19;
footballer.display();
footballer.playFootball();

return 0;
}

Output
My profession is: Teacher
My age is: 23
I can walk.
I can talk.
I can teach Maths.
My profession is: Footballer
30 | P a g e
My age is: 19
I can walk.
I can talk.
I can play Football.

36. C++ program for Multiple Inheritance.


#include<iostream.h>
#include<conio.h>
class Area
{
public:
float area_calc(float l,float b)
{
return l*b;
}
};

class Perimeter
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};

class Rectangle : private Area, private Perimeter


{
private:
float length, breadth;
public:
Rectangle() : length(0.0), breadth(0.0) { }
void get_data( )
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}

float area_calc()
{
return Area::area_calc(length,breadth);
}

float peri_calc()
{

31 | P a g e
return Perimeter::peri_calc(length,breadth);
}
};

int main()
{
clrscr();
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"\nPerimeter = "<<r.peri_calc();
getch();
return 0;
}

Output:
Enter length: 4
Enter breadth: 5
Area = 20
Perimeter = 18

37. Program for Hierarchical Inheritance.


#include<iostream.h>
#include<conio.h>
class A //Base Class
{
public:
int a,b;
void getnumber()
{
cout<<"\n\nEnter Number :::";
cin>>a;
}
};
class B : public A //Derived Class 1
{
public:
void square()
{
getnumber();
cout<<"\nSquare of the number :::"<<(a*a);
cout<<"\n-------------------------------";
}
};

class C : public A //Derived Class 2


32 | P a g e
{
public:
void cube()
{
getnumber();
cout<<"\nCube of the number :::"<<(a*a*a);
cout<<"\n-------------------------------";
}
};

int main()
{
clrscr();
B b1;
b1.square();
C c1;
c1.cube();
getch();
}

Output:

Enter Number :::10

Square of the number :::100


-------------------------------

Enter Number :::23

Cube of the number :::12167


-------------------------------

38. Program for MultiLevel Inheritance.

#include<iostream.h>
#include<conio.h>
class top
{
public:
int a;
void getdata()
{
cout<<"\n\nEnter first Number :::\t";
cin>>a;
}
void putdata()
{
cout<<"\nFirst Number Is :::\t"<<a;
33 | P a g e
}
};

//First level inheritance


class middle : public top
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"\n\nSquare Is :::"<<b;
}
};

//Second level inheritance


class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"\n\nCube ::: "<<c;
}
};

int main()
{
clrscr();
bottom b1;
b1.cube();
getch();
}

Output:
Enter first Number ::: 23
Square Is :::529
Cube ::: 12167

39. Program for Hybrid Inheritance.


#include<iostream.h>
#include<conio.h>
int a,b,c,d,e;
class A
34 | P a g e
{
public:
void getab()
{
cout<<"Enter A:";
cin>>a;
cout<<"Enter B:";
cin>>b;
}
};

class B:public A
{
public:
void getc()
{
cout<<"Enter C:";
cin>>c;
}
};

class C
{
public:
void getd()
{
cout<<"Enter D:";
cin>>d;
}
};

class D:public B,public C


{
public:
void result()
{
getab();
getc();
getd();
e=a+b+c+d;
cout<<"\n Addition is :"<<e;
}
};

void main()
{
clrscr();
D d1;
d1.result();

35 | P a g e
getch();
}

Output:

Enter A:10
Enter B:20
Enter C:30
Enter D:40

Addition is :100

40. Program for Addition of Two Matrices.

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int a[2][3], b[2][3], c[2][3];
int i, j;

for( i=0 ; i<2 ; i++ )


{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value of A : ";
cin >> a[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )


{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value of B : ";
cin >> b[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )


{
for( j=0 ; j<3 ; j++ )
{
c[ i ][ j ] = b[ i ][ j ] + a[ i ][ j ] ;
}
36 | P a g e
}

cout << "\n Matrix A is ::: \n";


for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << a[ i ][ j ] << " ";
}
cout << "\n";
}

cout << "\n Matrix B is ::: \n";


for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << b[ i ][ j ] << " ";
}
cout << "\n";
}

cout << "\n Matrix C is ::: \n";


for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << c[ i ][ j ] << " ";
}
cout << "\n";
}

getch();
}

Output:

Enter value of A : 10
Enter value of A : 20
Enter value of A : 30
Enter value of A : 40
Enter value of A : 50
Enter value of A : 60
Enter value of B : 10
Enter value of B : 20
Enter value of B : 30
Enter value of B : 40
Enter value of B : 50
Enter value of B : 60

37 | P a g e
Matrix A is :::
10 20 30
40 50 60

Matrix B is :::
10 20 30
40 50 60

Matrix C is :::
20 40 60
80 100 120

41. Program to find Transpose of given Matrix.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int a[2][3], b[3][2];
int i, j;

for( i=0 ; i<2 ; i++ )


{
for( j=0 ; j<3 ; j++ )
{
cout << "Enter value : ";
cin >> a[ i ][ j ];
}
}

for( i=0 ; i<2 ; i++ )


{
for( j=0 ; j<3 ; j++ )
{
b[ j ][ i ] = a[ i ][ j ] ;
}
}

cout << "\n Matrix A is ::: \n";


for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
cout << a[ i ][ j ] << " ";
38 | P a g e
}
cout << "\n";
}

cout << "\n Matrix B is ::: \n";


for( i=0 ; i<3 ; i++ )
{
for( j=0 ; j<2 ; j++ )
{
cout << b[ i ][ j ] << " ";
}
cout << "\n";
}

getch();
}

Output:

Enter value : 10
Enter value : 20
Enter value : 30
Enter value : 40
Enter value : 50
Enter value : 60

Matrix A is :::
10 20 30
40 50 60

Matrix B is :::
10 40
20 50
30 60

42. Program to explain Linear Search.


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int num[10], i, pos = -1, value;

cout << "Enter Ten Numbers ::: ";


for( i=0 ; i<10 ; i++ )
39 | P a g e
{
cin >> num[ i ];
}

cout << " Enter the number to be searched ::: ";


cin >> value;

for( i=0 ; i<10 ; i++ )


{
if( value == num[ i ] )
{
pos = i + 1;
break;
}
}

if( pos == -1 )
cout << "\n The element " << value << " not found.";
else
cout << "\n The position of " << value << " is ::: " << pos;

getch();
}

Output:

Enter Ten Numbers :::


10
20
30
40
50
60
70
80
90
100
Enter the number to be searched ::: 50
The position of 50 is ::: 5

43. Program to explain Binary Search.


#include <iostream.h>
#include <conio.h>

void main()
40 | P a g e
{
clrscr();
int num[10], i, beg, end, mid, pos = -1, value;

cout << "Enter Ten Numbers in Ascending Order::: ";


for( i=0 ; i<10 ; i++ )
{
cin >> num[i];
}

cout << " Enter the number to be searched ::: ";


cin >> value;

beg = 0;
end = 10 - 1;
while(beg <= end)
{
mid = (beg + end) / 2;
if( value == num[mid] )
{
pos = mid + 1;
break;
}
else if ( value >= num[mid] )
beg = mid + 1;
else
end = mid - 1;
}

if( pos == -1 )
cout << "\n The element " << value << " not found.";
else
cout << "\n The position of " << value << " is ::: " << pos;

getch();
}

Output:

Enter Ten Numbers in Ascending Order:::


1
2
3
41 | P a g e
4
5
6
7
8
9
10
Enter the number to be searched ::: 6
The position of 6 is ::: 6

44. Program to explain Bubble Sort.

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int i, j;
int n[ ] = { 30, 40, 50, 10, 20 };

cout << "\n Before Sorting :::";


for( i=0 ; i<5 ; i++ )
{
cout << " " << n[ i ];
}

for( i=0 ; i<5-1 ; i++ )


{
for( j=0 ; j<5-i-1 ; j++ )
{
if( n[ j ] > n[ j+1] )
{
int t = n[ j ];
n[ j ] = n[ j+1];
n[ j+1] = t;
}
}
}

42 | P a g e
cout << "\n\n After Sorting :::";
for( i=0 ; i<5 ; i++ )
{
cout << " " << n[ i ];
}

getch();
}

Output:

Before Sorting ::: 30 40 50 10 20

After Sorting ::: 10 20 30 40 50

45. Program to explain Selection Sort.

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int i, j, small, pos, tmp ;
int n[ ] = { 30, 40, 50, 10, 20 };

cout << "\n Before Sorting :::";


for( i=0 ; i<5 ; i++ )
{
cout << " " << n[ i ];
}

for( i=0 ; i<5 ; i++ )


{
small = n[ i ];
pos = i;
for( j=i+1 ; j<5 ; j++ )
{
if( n[ j ] < small )
{
small = n[ j ];
pos = j;
43 | P a g e
}
}
tmp = n[ i ];
n[ i ] = n[pos];
n[pos] = tmp;
}

cout << "\n\n After Sorting :::";


for( i=0 ; i<5 ; i++ )
{
cout << " " << n[ i ];
}

getch();
}

Output:
Before Sorting ::: 30 40 50 10 20
After Sorting ::: 10 20 30 40 50
46. Program to explain Insertion Sort

#include <iostream.h>
#include <conio.h>
#include <limits.h>

void main()
{
clrscr();
int i, j, tmp;
int n[ ] = { 0, 30, 40, 50, 10, 20 };

cout << "\n Before Sorting :::";


for( i=1 ; i<=5 ; i++ )
{
cout << " " << n[ i ];
}

n[0] = INT_MIN;
for( i=1 ; i<=5 ; i++ )
{
tmp = n[ i ];
j = i - 1;
while( tmp < n[ j ] )
44 | P a g e
{
n[ j+1] = n[ j ];
j--;
}
n[ j+1] = tmp;
}

cout << "\n\n After Sorting :::";


for( i=1 ; i<=5 ; i++ )
{
cout << " " << n[ i ];
}
getch();
}

Output:
Before Sorting ::: 30 40 50 10 20
After Sorting ::: 10 20 30 40 50

47. Program to Implement Merge Sort

#include <iostream>
using namespace std;
#include <conio.h>
void merge(int *,int, int , int );
void mergesort(int *a, int low, int high)
{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
45 | P a g e
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int a[20], i, b[20];
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>a[i];
}
46 | P a g e
mergesort(a, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<a[i];
}
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<b[i];
}
getch();
}

Output:
enter the elements
78
45
80
32
67
sorted array
32
45
67
78
80

47 | P a g e

You might also like