C++ Practical Program
C++ Practical Program
C++ Practical Program
Name: _________________________
AIM:
PROGRAM:
#include <iostream.h>
#include <conio.h>
class funOverloader {
public:
};
int main() {
funOverloader obj;
obj.print(3, 5);
obj.print(2.5, 4.0);
obj.print("Hello, Overload!");
getch();
return 0;
}
OUTPUT:
Sum=8
Product=9.2
String=Hello World!
RESULT:
AIM:
PROGRAM:
#include <iostream.h>
#include <conio.h>
if (age > 0) {
int main() {
greetUser();
greetUser("Alice");
greetUser("Bob", 25);
getch();
return 0;
OUTPUT:
Hello,Guest
Hello,Alice
Hello,Bob.age:25
PROGRAM 3: INLINE FUNCTION
Aim:
To write a c++ program for finding square number using Inline Function.
PROGRAM:
#include <iostream.h>
#include<conio.h>
void main() {
getch();
OUTPUT:
Max(20,10): 20
Max(0,200): 200
Max(100,1010): 1010
RESULT:
Aim:
Program:
#include <iostream.h>
class Rectangle {
private:
int length;
int width;
public:
Rectangle(int l, int w) {
length = l;
width = w;
}
int calculateArea() {
return length * width;
}
int calculatePerimeter() {
return 2 * (length + width);
}
};
int main() {
return 0;
}
Output:
Area of rect1: 20
Perimeter of rect1: 18
Area of rect2: 21
Perimeter of rect2: 20
Aim:
To write a c++ program for finding complex number using the concept of Passing Object Functions.
Program:
#include<iostream.h>
#include<conio.h>
class complex{
int a;
int b;
public:
a = v1;
b = v2;
a = o1.a + o2.a;
b = o1.b + o2.b;
void printNumber(){
};
int main(){
c1.setData(1, 2);
c1.printNumber();
c2.setData(3, 4);
c2.printNumber();
c3.setDataBySum(c1, c2);
c3.printNumber();
getch();
clrscr();
return 0;
Output:
Aim:
To write a c++ program to calculate the length of given box using friend function.
Program:
#include<iostream.h>
#include<conio.h>
class Box
private:
int length;
public:
Box():length(0){}
};
int printLength(Box b)
b.length+=10;
return b.length;
void main()
Box b;
cout<<"Length of box="<<printLength(b)<<endl;
getch();
Output:
Length of box=10
Program 7: “this “ POINTER
Aim:
Program:
#include<iostream.h>
#include<conio.h>
class Test
private:
int x;
public:
void setX(int x)
this->x=x;
void print()
cout<<"x="<<x<<endl;
};
int main()
Test obj;
int x=20;
obj.setX(x);
obj.print();
getch();
return 0;
}
Output:
X=20
Result:
Program 8: CONSTRUCTOR AND DESTRUCTOR
Aim:
To write a c++ program to calculate simple function using the concepts of constructor and destructor.
Program:
#include <iostream.h>
#include<conio.h>
class Calculator
private:
int num1;
int num2;
public:
Calculator(int a, int b)
num1 = a;
num2 = b;
cout << "Calculator object created with numbers: " << num1 << " and " << num2 << endl;
~Calculator()
int add()
return num1+num2;
}
int subtract()
return num1-num2;
int multiply()
return num1*num2;
};
int main() {
getch();
return 0;
Output:
Addition result:15
Subtraction result:5
Multiplication result:50
To write a c++ program Unary prefix & postfix increment operator overloading (++).
Program:
#include <iostream.h>
#include<conio.h>
class Counter {
private:
int count;
public:
Counter() : count(0) {}
Counter& operator++() {
++count;
return *this;
Counter operator++(int) {
++(*this);
return temp;
};
int main() {
Counter c;
++c;
c.display();
c++;
c.display();
getch();
clrscr();
return 0;
Output:
Count : 1
Count : 2
To write a c++ program for Add two complex numbers using binary '+' operator overloading
Program:
#include <iostream.h>
#include<conio.h>
class Complex {
private:
double real;
double imag;
public:
Complex temp;
return temp;
void display() {
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex result;
result = c1 + c2;
result.display();
getch();
clrscr();
return 0;
Output:
To write a c++ program to find product of two numbers using single inheritance.
Program:
#include <iostream.h>
#include<conio.h>
public:
int x;
void getdata()
};
private:
int y;
public:
void readdata()
void product()
};
int main()
derive a;
a.getdata();
a.readdata();
a.product();
getch();
clrscr();
return 0;
Output:
Product= 50
Program:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class Student
protected:
char *name;
int rollNo;
public:
void getStudent() {
void displayStudent() {
};
protected:
int marks[3];
public:
void getMarks()
{
for(int i=0;i<3;i++)
cin >>marks[i];
void displayMarks()
cout << "Subject " << i + 1 << ": " << marks[i] << endl;
};
public:
void calculateTotal()
int total = 0;
total += marks[i];
}
};
int main()
Result student;
student.getStudent();
student.getMarks();
student.displayStudent();
student.displayMarks();
student.calculateTotal();
getch();
return 0;
Output:
Subject 1: 55
Subject 2: 67
Subject 3: 89
_______________________________________________
Name: anu
Roll number:11
Marks obtained:
Subject 1: 55
Subject 2: 67
Subject 3: 89
To write a c++ program for employee salary management using multiple inheritance.
Program:
#include<iostream.h>
#include<conio.h>
class Employee
protected:
char *name;
int empId;
public:
void getEmployee()
};
class Salary1
protected:
int basicSal;
int allowance;
public:
void getSalary()
};
public:
void displaySalary()
};
int main()
SalaryManagement emp;
emp.getEmployee();
emp.getSalary();
emp.displaySalary();
getch();
return 0;
}
Output:
Enter allowance:500
___________________________________________
Employee ID:12
Total salary:7500
To write a c++ program for Hostel Management system using hierarchical inheritance
Program:
#include <iostream.h>
#include <string.h>
#include<conio.h>
class Person {
protected:
char *name;
int age;
public:
name = n;
age = a;
void displayData() {
};
protected:
int rollNo;
public:
void setRollNo(int r) {
rollNo = r;
void displayRollNo() {
};
protected:
char *hostelName;
int roomNo;
public:
hostelName = h;
roomNo = r;
void displayHostel() {
};
int main() {
HostelStudent hs;
hs.setData("John", 20);
hs.setRollNo(101);
hs.displayData();
hs.displayRollNo();
hs.displayHostel();
getch();
return 0;
}
Output:
Student Details:
Name:John
Age:20
Roll No:101
Room no:301
Program:
#include <iostream.h>
#include<conio.h>
class Base1
public:
void display() {
};
class Derived1
public:
void display() {
};
public:
void display() {
};
{
public:
void display() {
};
int main()
Derived3 obj;
obj.Base1::display();
obj.Derived1::display();
obj.Derived2::display();
obj.display();
getch();
return 0;
Output:
To write a c++ program for Book shop management using virtual function.
Program:
#include <iostream.h>
#include <string.h>
#include<conio.h>
class Book
protected:
char *title;
char *author;
float price;
public:
// Constructor
};
private:
char *genre;
public:
FictionBook(char *t,char *a, float p, char *g) : Book(t, a, p), genre(g) {}
void display(){
Book::display();
};
// Derived class
private:
char *subject;
public:
// Constructor
NonFictionBook(char *t, char *a, float p, char *s) : Book(t, a, p), subject(s) {}
void display(){
Book::display();
};
int main() {
fictionBook.display();
cout << "\nNon-Fiction Book Details:" << endl;
nonFictionBook.display();
getch();
return 0;
Output:
Price:$10.99
Genre:Classic
Price:$15.99
Genre:Anthropology
To write a c++ program to create and manipulate a Text File using Text file.
Program:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
ofstream outputFile("example.txt");
if(outputFile){
outputFile.close();
else{
return 1;
ifstream inputFile("example.txt");
char line[255];
if(inputFile){
cout<<"contents of 'example.txt':"<<endl;
while(inputFile.getline(line,255)){
cout<<line<<endl;
inputFile.close();
}else
getch();
return 0;
Output:
Contents of ‘example.txt’:
Program:
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main(){
clrscr();
ofstream outFile("output.txt");
if(!outFile){
return 1;
outFile<<"Hello,World!"<<endl;
outFile.close();
ifstream inFile("output.txt");
if(!inFile){
return 1;
char line[255];
while(inFile){
inFile.getline(line,255);
cout<<line<<endl;
inFile.close();
getch();
return 0;
Output:
contents of 'example.txt':
Result:
To write a c++ program to find the Biggest of three numbers using Command Line Arguments.
Program:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
if(argc !=4)
return 1;
int num1=atoi(argv[1]);
int num2=atoi(argv[2]);
int num3=atoi(argv[3]);
biggest=num2;
biggest=num3;
getch();
return 0;
Output:
C:\TURBOC3\BIN>CD..
C:\TURBOC3<CD SOURCE
C:\TURBOC3\SOURCE>commandargu.exe 12 22 15
To write a c++ program for printing various type of data using one class template.
Program:
#include<iostream.h>
#include<conio.h>
class Pair{
private:
T1 first;
T2 second;
public:
first =f;
second =s;
T1 getFirst() const{
return first;
T2 getSecond() const{
return second;
first=f;
second=s;
cout<<"("<<first<<","<<second<<")"<<endl;
}
};
int main(){
Pair<int,int>intPair(10,20);
cout<<"integer Pair:";
intPair.display();
Pair<double,char>doubleCharPair(3.14,'A');
cout<<"Double-char Pair:";
doubleCharPair.display();
getch();
clrscr();
return 0;
Output:
Integer pair(10,20)
Double-char pair(3.14, A)
To write a c++ program for finding maximum of different type of data using one Function Template.
Program:
#include<iostream.h>
#include<conio.h>
template<class T>
return (a>b)?a:b;
int main(){
cout<<"Max of"<<num1<<"and"<<num2<<"is;"<<max(num1,num2)<<endl;
double num3=15.5,num4=25.5;
cout<<"Max of"<<num3<<"and"<<num4<<"is;"<<max(num3,num4)<<endl;
cout<<"Max of"<<char1<<"and"<<char2<<"is;"<<max(char1,char2)<<endl;
getch();
return 0;
Output:
Aim:
To write a c++ program for finding exception(divided by zero) using exception handling.
Program:
#include <iostream.h>
int main() {
try {
if (denominator == 0) {
return 0;
}
Output:
Enter numerator: 15
Enter denominator: 0