0% found this document useful (0 votes)
72 views

Complete C++ Practical File

The document provides 20 solutions to C++ programming problems involving classes, objects, inheritance and other OOP concepts. Some of the problems include: adding two complex numbers using a class, adding two times using a class, function overloading, single inheritance, multiple inheritance, and using friend functions. The solutions demonstrate how to define classes and subclasses, declare objects, overload functions, use inheritance and access specifiers, and other core OOP features in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Complete C++ Practical File

The document provides 20 solutions to C++ programming problems involving classes, objects, inheritance and other OOP concepts. Some of the problems include: adding two complex numbers using a class, adding two times using a class, function overloading, single inheritance, multiple inheritance, and using friend functions. The solutions demonstrate how to define classes and subclasses, declare objects, overload functions, use inheritance and access specifiers, and other core OOP features in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

1.

Write a C++ program to print the following lines:


i) Your introduction
ii) your institute introduction
Solution:
#include<iostream.h>
#include<conio.h>

void main()
{
cout<<”Your introduction”;
cout<<”your institute introduction” ;

getch();
}

Output :
2. write a c++ program that prompts the user to input three integer value and print these
values in forward and reversed order.
Solution :

#include<iostream.h>
#include<conio.h>
Void main()
{
int num,rev;
clrscr();
cout<<”enter the 3 digit integer number :”;
cin>>num;
cout<<”entered number :”<<num;
cout<<”reverse :”
while(num!=0)
{
rev=num%10;
num=num/10;
cout<<rev;
}
getch();
}
Output:
3. Write a program to display the following output using a single cout statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69

Solution :

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

void main()
{
clrscr();

cout << "Subjects Marks\nMathematics 90\nComputer 77\nchemistry 69\n";

getch();
}

Output :
4. Write a program which accept principle, rate and time from user and print the simple
interest.

Solution :

#include<iostream.h>
#include<conio.h>
void main()
{
int principle,rate,time,interest;
clrscr();

cout<<" Enter Principle : ";


cin>>principle;
cout<<endl;
cout<<" Enter Rate : ";
cin>>rate;
cout<<endl;
cout<<" Enter Time : ";
cin>>time;
cout<<endl;
interest = (principle * rate * time)/100;
cout<<" Simple interest is : "<< interest;

getch();
}

Output :
5. Write a program to swap the values of two variables.

Solution :

#include<iostream.h>
#include<conio.h>
void main()
{
int x, y,temp;
cout<<"Enter Value of x :";
cin>>x;
cout<<"\nEnter Value of y :";
cin>>y;
temp = x;
x = y;
y = temp;

cout<<”\nAfter Swapping: x=“ <<x”,y=”<< y;


getch();
}

Output :
5. Write a program to check whether the given number is even or odd (using?: ternary
Operator).
Solution :

#include<stdio.h>
#include<conio.h>

void main()
{
int n;

cout << "Enter an integer: ";


cin >> n;

(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";

getch();
}

Output :
7.Write a program to swap value of two variables without using third variable..

Solution :

#include<stdio.h>
#include<conio.h>

void main()
{
int a=5, b=10;
cout<<"Before swap a= "<<a<<" b= "<<b<<endl;

a=a+b; //a=15 (5+10)


b=a-b; //b=5 (15-10)
a=a-b; //a=10 (15-5)

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

getch();
}

Output :
9.Write a C++ program to add two complex numbers using class.

Solution :

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

class Complex {
public:
int real;
int imag;

Complex()
{
real = 0;
imag = 0;
}
Complex(int r, int i)
{
real = r;
imag = i;
}

Complex Addition(Complex C1, Complex C2)


{

Complex res;

res.real = C1.real + C2.real;

res.imag = C1.imag + C2.imag;

return res;
}
};

void main()
{

Complex C1(4, 5);

cout << "Complex number 1 : " << C1.real << " + i" << C1.imag << endl;

Complex C2(8, 9);


cout << "Complex number 2 : " << C2.real << " + i" << C2.imag << endl;

Complex C3;

C3 = Addition(C1, C2);

cout << "Sum of complex no : real :“ << C3.real << " , imag:" << C3.imag;
cout << endl;

getch();
}

Output :
9. Write a c++ program to Adding two times using class.

Solution :

#include<iosteam.h>
#include<conio.h>

class Time {
private:
int hours;
int minutes;
int seconds;

public:
void getTime(void);
void putTime(void);
void addTime(Time T1, Time T2);
};

void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours? ";
cin >> hours;
cout << "Minutes? ";
cin >> minutes;
cout << "Seconds? ";
cin >> seconds;
}

void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl;
}

void Time::addTime(Time T1, Time T2)


{
this->seconds = T1.seconds + T2.seconds;
this->minutes = T1.minutes + T2.minutes + this->seconds / 60;

this->hours = T1.hours + T2.hours + (this->minutes / 60);


this->minutes %= 60;
this->seconds %= 60;
}

void main()
{
Time T1, T2, T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1, T2);
T3.putTime();

getch();
}

Output :

10. Write a C++ program for function overloading.


Solution :

#include<stdio.h>
#include<conio.h>

void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char const *c) {
cout << " Here is char* " << c << endl;
}
void main()
{
print(10);
print(10.10);
print("ten");

getch();
}

Output :

11. Write a program for single inheritance.


Solution :

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

class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

class Car : public Vehicle {


};

void main()
{
Car obj;

getch();
}

Output :

12. Write a program for multiple inheritance.


Solution :

#include<stdio.h>
#include<conio.h>

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle\n";
}
};

class Car : public Vehicle, public FourWheeler {


};

void main()
{
Car obj;

getch();
}

Output :

13. Write a program for multilevel inheritance.


Solution :

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

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

class fourWheeler : public Vehicle {


public:
fourWheeler()
{
cout << "Objects with 4 wheels are vehicles\n";
}
};
class Car : public fourWheeler {
public:
Car() { cout << "Car has 4 Wheels\n"; }
};

void main()
{
Car obj;

getch();
}
Output :

14. Write a program for Hierarchical Inheritance.


Solution :

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

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

class Car : public Vehicle {


};

class Bus : public Vehicle {


};

void main()
{

Car obj1;
Bus obj2;

getch();
}

Output :

15. Write a program for Hybrid inheritance.


Solution :

#include<stdio.h>
#include<iostream.h>

class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};

class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};

class Car : public Vehicle {


};

class Bus : public Vehicle, public Fare {


};

void main()
{
Bus obj2;

getch();
}
Output :
16. Write a program for copy constructor.

Solution :

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

class Wall {
private:
double length;
double height;

public:

Wall(double len, double hgt) {


length = len;
height = hgt;
}

Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}

double calculateArea() {
return length * height;
}
};

void main()
{
Wall wall1(10.5, 8.6);
Wall wall2 = wall1;
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();

getch();
}
Output :
17. Write a program for parameterised.

Solution :

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

class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() {
return length * height;
}
};
void main() {
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);

cout << "Area of Wall 1: " << wall1.calculateArea() << endl;


cout << "Area of Wall 2: " << wall2.calculateArea();

getch();
}
Output :
18. Write a program for default constructor.

Solution :

#include<stdio.h>
#include<conio.h>

class Wall {
private:
double length;

public:
Wall() {
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};

void main()
{
Wall wall1;

getch();
}

Output :
19. Write a program for dynamic constructor.

Solution :

#include<stdio.h>
#include<conio.h>

class geeks {
const char* p;

public:
geeks()
{
p = new char[6];
p = "geeks";
}

void display() { cout << p << endl; }


};

void main()
{
geeks obj;
obj.display();
}
getch();
}

Output :
20. Write a program for friend funtion.

Solution :

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

class demo
{
private:
int a=5;
friend void display();
};
void display()
{
cout<<"a: "<<d.a<<endl;
}
void main()
{
demo d;
display(d);

getch();
}

Output :

You might also like