0% found this document useful (0 votes)
2 views25 pages

C++ Programming Examples and Outputs

The document contains a series of C++ programming exercises that cover various concepts such as loops, functions, classes, inheritance, operator overloading, and more. Each exercise includes code snippets along with their expected output. The exercises aim to demonstrate fundamental programming techniques and object-oriented programming principles.

Uploaded by

riya9a20002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views25 pages

C++ Programming Examples and Outputs

The document contains a series of C++ programming exercises that cover various concepts such as loops, functions, classes, inheritance, operator overloading, and more. Each exercise includes code snippets along with their expected output. The exercises aim to demonstrate fundamental programming techniques and object-oriented programming principles.

Uploaded by

riya9a20002
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link] a program to print name 10 times.

#include<iostream.h>
#include<conio.h>
int main()
{
int i;
clrscr();
for(i=0;i<10;i++)
{
cout<<"kanhaiya"<<"\n";
}
getch();
}

OUTPUT:
Kanhaiya
kanhaiya
kanhaiya
kanhaiya
kanhaiya
kanhaiya
kanhaiya
kanhaiya
kanhaiya
kanhaiya

[Link] a program to find area of rectangle & circle using


function

#include<iostream.h>
#include<conio.h>
#define pie 3.14
void rectangle()
{
int l,b,area;
clrscr();
cout<<"Enter the length :"<<"\n" ;
cin>>l;
cout<<"Enter the breadth : "<<"\n";
cin>>b;
area=l*b;
cout<<"The area of rectangle"<<area<<"\n";
};
void circle()
{
float r,area;
cout<<"Enter the radius :"<<"\n";
cin>>r;
area=pie*r*r;
cout<<"The area of circle :"<<area<<"\n" ;
};
int main()
{
cout<<"The area of rectangle & circle is :"<<"\n";
rectangle();
circle();
getch();
}

OUTPUT

Enter the length : 6


Enter the breadth: 4
The area of rectangle 24
Enter the radius :
5
The area of circle : 78.5

Q3. Write a program solve quadric equation.


#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
int a,b,c,p,e;
float ans;
clrscr();
cout<<"a :";
cin>>a;
cout<<"\nb :";
cin>>b;
cout<<"\nc :";
cin>>c;
p=2*a;
e=sqrt(b*b-2*c*p);
if(e>0)
{
ans=((-b)/p)+(e/p);
cout<<"The answer of the equation is : "<<ans;
}
else
{
ans=((-b)/p)-(e/p);
cout<<"The answer of the equation is : "<<ans;
}
getch();
}
OUTPUT:

a:1
b : -4
c:4
The answer of the equation is : 2
Q4. Write a program to find highest & lowest value from array .

#include <iostream.h>
#include <conio.h>
using namespace std;
int main()
{
int a[50], i, n, high, low;
cout << "\n Enter the number of elements: ";
cin >> n;

cout << "\n Input the array elements: ";


for (i = 0; i < n; i++)
{
cin >> a[i];
}
high = low = a[0]; // Initialize both with first element

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


{
if (a[i] > high)
high = a[i];

if (a[i] < low)


low = a[i];
}
cout << "\n The lowest element is: " << low;
cout << "\n The highest element is: " << high;
getch();
}

OUTPUT
Enter the number of elements : 4
3
5
7
The lowest element is : 3
The highest element is : 7

Q5. Write a program of inline function.

#include <iostream.h>
#include <conio.h>
inline float mul(float x, float y)
{
float a = x * y;
return a;
}

inline double div(double p, double q)


{
double b = p / q;
return b;
}

int main()
{
float r = 2.14;
float s = 3.25;

cout << mul(r, s) << "\n";


cout << div(r, s) << "\n";

getch();

OUTPUT
6.955
0.658462

Q6. Write a program to show the working of class and object.

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

class item
{
int number;
float cost;

public:
void getdata(int a, float b);
void putdata(void);
};

void item::getdata(int a, float b)


{
number = a;
cost = b;
}

void item::putdata(void)
{
cout << "Number : " << number << "\n";
cout << "Cost : " << cost << "\n";
}

int main()
{
item x;

[Link](100, 281.4);
[Link]();

getch();

OUTPUT:
Number : 100
Cost : 281.399

Q7. Write a program with name employee with take 2 employee


their name ,D.O.B, Age,Year of service, salary and their values.

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

class employee
{
int m;
int i;
int n;
char p[5];
char q[5];

public:
void name(void);
void dob(void);
void age(void);
void yos(void);
void salary(void);
};

void employee::name(void)
{
cout << "\nEnter the name of the employee: ";
for (i = 0; i < 5; i++)
cin >> p[i]>>q[i];
cout << "\nThe name of the employee is: ";
for (i = 0; i < 5; i++)
cout << p[i]<<q[i];
}

void employee::dob(void)
{
cout << "\nEnter the D.O.B (in number format): ";
cin >> m>> n;
cout << "D.O.B: " << m<<endl<< n;
}

void employee::age(void)
{
cout << "\nEnter the age: ";
cin >> n>>m;
cout << "Age: " << n<<endl<< m;
}

void employee::yos(void)
{
cout << "\nEnter the year of service: ";
cin >> p>>m;
cout << "Year of Service: " << p<<endl<< m;
}

void employee::salary(void)
{
cout << "\nEnter the salary: ";
cin >> m>>n;
cout << "Salary: " << m<<endl<< n;
}

int main()
{
clrscr();
employee a;

[Link]();
[Link]();
[Link]();
[Link]();
[Link]();

getch();
}

OUTPUT:
Enter the name of two employee : suman
Kanha
The name of the employees are : suman kanha
Enter the D.O.B (in number format): 2007
2006
D.O.B : 2007
2006
Enter the age : 18
19
Enter the year of service : 3
2
Year of service : 3
2
Enter the salary : 10000
8000

Q8. Declare a class Max which has 3 integer type member . A


function read to input the values
data member and function
display the greater value from them.

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

class max
{
public:
int num1, num2, num3;

void read()
{
cout << "Enter three numbers: " << "\n";
cin >> num1 >> num2 >> num3;
}

void display()
{
if (num1 > num2 && num1 > num3)
{
cout << "First number is greatest." << endl
<< "Which is = " << num1;
}
else if (num2 > num1 && num2 > num3)
{
cout << "Second number is greatest." << endl
<< "Which is = " << num2;
}
else
{
cout << "Third number is greatest." << endl
<< "Which is = " << num3;
}
}
};

int main()
{
clrscr();
max m;
[Link]();
[Link]();
getch();
}

OUTPUT:
Enter three numbers :3
9
1
Second number is greatest.
Which is = 9

Q9. Write a program using friend function .


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

class sample
{
int a;
int b;

public:
void setvalue()
{
a = 25;
b = 40;
}

friend float mean(sample s);


};
float mean(sample s)
{
return float(s.a + s.b) / 2.0;
}

int main()
{
clrscr();
sample x;
[Link]();

cout << "Mean value: " << mean(x) << "\n";

getch();
}

OUTPUT:
Mean value: 32.5

Q10. Write a program friend function.

#include <iostream.h>
#include <conio.h>
class myclass
{
int x;

public:
void setx(int i)
{
x = i;
}

int getx()
{
return x;
}
};

int main()
{
clrscr();
myclass obs[4];
int i;

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


{
obs[i].setx(i);
}

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


{
cout << "obs[" << i << "].getx(): " << obs[i].getx() << "\n";
}

getch();
}

OUTPUT :
Obs[0].getc() : 0
Obs[1].getc() : 1
Obs[2].getc() : 2
Obs[3].getc() : 3

Q11. WAP to Constructor.


#include <iostream>
using namespace std;

class Employee
{
public:
// Constructor
Employee()
{
cout << "Default constructor invoked" << endl;
}
};

int main()
{
Employee e1;
Employee e2;
return 0;
}

OUTPUT
Default constructor invoked
Default constructor invoked

Q12. WAP to Destructor.

#include <iostream>
using namespace std;
class Demo
{
private:
int num1, num2;
public:
Demo(int n1, int n2)
{
cout << "Inside Constructor" << endl;
num1 = n1;
num2 = n2;
}
void display()
{
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
}
~Demo()
{
cout << "Inside Destructor" << endl;
}
};
int main()
{
Demo obj1(10, 20);
[Link]();
return 0;
}

OUTPUT
Inside Constructor
num1 = 10
num2 = 20
Inside Destructor

Q13. WAP of Single Inheritance.

#include <iostream>
using namespace std;
class Account
{
public:
float salary = 60000;
};
class Programmer : public Account
{
public:
float bonus = 5000;
};
int main()
{
Programmer p1;
cout << "Salary: " << [Link] << endl;
cout << "Bonus: " << [Link] << endl;
return 0;
}

OUTPUT
Salary: 60000
Bonus: 5000

[Link] of Multiple Inheritance.

#include <iostream>
using namespace std;
// Base class 1
class Base1
{
public:
void display1()
{
cout << "This is the first function of the Base class" << endl;
}
};
// Base class 2
class Base2
{
public:
void display2()
{
cout << "This is the second function of the Base class" << endl;
}
};
// Derived class (child of Base1 and Base2)
class Child : public Base1, public Base2
{
public:
void display3()
{
cout << "This is the function of the Derived (Child) class" << endl;
}
};
int main()
{
Child ch;
// Calling member functions from both base classes and child class
ch.display1(); // From Base1
ch.display2(); // From Base2
ch.display3(); // From Child
return 0;
}

OUTPUT
This is the first function of the Base class
This is the second function of the Base class
This is the function of the Derived (Child) class

[Link] of Multilevel Inheritance

#include <iostream>
using namespace std;
// Base class
class Base
{
public:
int x;
void getData()
{
cout << "Enter value of x: ";
cin >> x;
}
};
// Derived class 1 (inherits from Base)
class Derived1 : public Base
{
public:
int y;
void readData()
{
cout << "Enter value of y: ";
cin >> y;
}
};
// Derived class 2 (inherits from Derived1)
class Derived2 : public Derived1
{
private:
int z;
public:
void inData()
{
cout << "Enter value of z: ";
cin >> z;
}
void product()
{
cout << "Product = " << x * y * z << endl;
}
};
int main()
{
Derived2 a;
[Link]();
[Link]();
[Link]();
[Link]();
return 0;
}

OUTPUT
Enter value of x: 2
Enter value of y: 5
Enter value of z: 6
Product = 60

Q16. WAP of Hybrid Inheritance.

#include <iostream>
using namespace std;
// Base class
class Animal
{
public:
Animal()
{
cout << "This is an Animal" << endl;
}
};
// Derived class 1 using virtual inheritance
class Mammal : virtual public Animal
{
public:
Mammal()
{
cout << "This is a Mammal" << endl;
}
};
// Derived class 2 using virtual inheritance
class Herbivore : virtual public Animal
{
public:
Herbivore()
{
cout << "This is a Herbivore" << endl;
}
};
// Derived class (Hybrid Inheritance)
class Cow : public Mammal, public Herbivore
{
public:
Cow()
{
cout << "A Cow is a Herbivorous Mammal" << endl;
}
};
int main()
{
Cow c;
return 0;
}

OUTPUT
This is an Animal
This is a Mammal
This is a Herbivore
A Cow is a Herbivorous Mammal

Q17. WAP of Hierarchical Inheritance.

#include <iostream>
using namespace std;
// Base class
class A
{
public:
int x, y;
void getdata()
{
cout << "Enter values of x and y: ";
cin >> x >> y;
}
};
// Derived class 1
class B : public A
{
public:
void product()
{
cout << "Product = " << x * y << endl;
}
};
// Derived class 2
class C : public A
{
public:
void sum()
{
cout << "Sum = " << x + y << endl;
}
};
int main()
{
B obj1;
C obj2;
[Link](); // Input x and y
[Link](); // From class B
obj2.x = obj1.x; // Copy values
obj2.y = obj1.y;
[Link](); // From class C

return 0;
}

OUTPUT
Enter values of x and y: 56
36
Product = 2016
Sum = 92
Q18. WAP of function overloading.

#include <iostream>
using namespace std;
class Calc
{
public:
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
};
int main()
{
cout << Calc::add(10, 20) << endl;
cout << Calc::add(12, 20, 23) << endl;
return 0;
}

OUTPUT
30
55
Q19. WAP to Resolution operator.
#include <iostream>
using namespace std;
int num = 50; // Global variable
int main()
{
int num = 100; // Local variable
cout << "The value of the local variable num = " << num << endl;
cout << "The value of the global variable num = " << ::num << endl;
return 0;
}

OUTPUT
The value of the local variable num = 100
The value of the global variable num = 50

Q20. WAP of operator Overloading.

#include <iostream>
using namespace std;
class Test
{
private:
int count;
public:
// Constructor
Test() : count(5) {}
// Operator overloading for pre-decrement
void operator--()
{
count = count - 3;
}
void display()
{
cout << "Count: " << count << endl;
}
};
int main()
{
Test tc;
--tc; // Calls overloaded operator--
[Link](); // Displays the result

return 0;
}

OUTPUT
2

You might also like