Shah Satnam Ji Boys
College, Sirsa
Practical file
Of
Programming in C++
Submittd To: SubmittedBy:
Mr. Mohit Aadish Kumar
Asst.Professor Class: BCA 2nd SEM
(Dpt.of com. science) Univ. Roll No.
23008115470012
INDEX
Sr.no Program Name Signature
1 Write a Program to calculate the value of an expression
( a+b)2 = a2 + b2 + 2ab
2 Write a program findout factorial of a number.
3 Write a program to find largest no. of out of 3 number
4 Write a program to class implementation(simple class and
object
5 Write a program wsapping two number without using 3rd
variable
6 Write a program a table of given number
7 Write a program to add give time using objects as argument
8 Write a program class with constructor
9 Write a program multilevel inheritance
10 Write a program hybrid inheritance
11 Write a program to illustrates the use of pointers of objects
12 Write a program virtual function
13 Write a program working with single line
14 Write a program with multiple files
// program to show (a+b)2 = a2+b2+2ab
#include<iostream>
using namespace std;
int main()
int a, b, result;
cout<<"Enter a value :";
cin>>a;
cout<<"Enter b value :";
cin>>b;
result = a*a+b*b+2*a*b;
cout<<"(a+b)^2 of two numbers : "<<result<<endl;
return 0;
}
Output
// Write a program to findout factorial of a number
#include<iostream>
using namespace std;
int main()
int i, fact=1, number;
cout<<"Enter any number :";
cin>>number;
for(i=1; i<=number; i++)
fact=fact*i;
cout<<"Factorial of" <<number<<"is :"<<fact<<endl;
return 0;
}
Output
// write a program to find largest number among three numbers
#include<iostream>
using namespace std;
int main()
double n1, n2, n3;
cout <<"Enter three numbers :";
cin >>n1 >>n2 >> n3;
if(n1 >=n2 && n1 >= n3)
cout <<"Largest number :" <<n1;
else if(n2 >=n1 && n2 >= n3)
cout <<"Largest number :" <<n2;
else
cout <<"Largest number :" <<n3;
return 0;
}
Output
// Write a program to class implementation ( simple class and objects)
#include<iostream>
using namespace std;
class Rect{
public:
double length;
double breadth;
double calculateArea(){
return length * breadth;
};
int main()
Rect rect1;
rect1.length = 32.5;
rect1.breadth = 20.8;
cout<<"The area of rectangle :"<<rect1.calculateArea()<<endl;
return 0;
}
Output
// Write a program to add give time using objects as arguments
#include <iostream>
class Time {
private:
int hours;
int minutes;
public:
Time(int h, int m) {
hours = h;
minutes = m;
void addTime(Time t) {
minutes += t.minutes;
hours += t.hours + minutes / 60;
minutes %= 60;
hours %= 24;
void displayTime() {
std::cout << "Time: " << hours << " hours " << minutes << " minutes" <<
std::endl;
};
int main() {
Time t1(3, 45);
Time t2(1, 30);
std::cout << "Before addition:" << std::endl;
t1.displayTime();
t2.displayTime();
t1.addTime(t2);
std::cout << "\nAfter addition:" << std::endl;
t1.displayTime();
return 0;
}
Output
// Write a program swapping two number without using 3rd (temporary)
variable
#include<iostream>
using namespace std;
int main()
int a,b;
cout <<"Enter the first number :";
cin>>a;
cout<<"Enter the second number :";
cin>>b;
cout<<"\n\nValue before Swapping : \n "<<endl;
cout<<"First Number ="<<a<<endl;
cout<<"Second Number ="<<b<<endl;
a = a + b;
b = a - b;
a = a - b;
cout<<"\n\nValue After Swapping :\n"<<endl;
cout<<"First Number ="<<a<<endl;
cout<<"Second Number ="<<b<<endl;
cout<<"\n\n\n";
return 0;
}
Output
// Write a program to print a table of given number
#include<iostream>
using namespace std;
int main()
int number;
cout<<"Enter the given number :"<<endl;
cin>>number;
for(int i=1; i<=10; i++)
cout<<number<<"*"<<i<<"="<<number*i<<endl;
Return 0;
}
Output
// Write a program class with constructor
#include<iostream>
using namespace std;
class student {
int rno;
char name[50];
double fee;
public:
student()
cout << "Enter the Rollno : ";
cin >> rno;
cout << "Enter the Name : ";
cin >> name;
cout << "Enter the Fee : ";
cin >> fee;
void display()
cout << endl << rno << "\t" << name << "\t" << fee;
}
};
int main()
student s;
s.display();
return 0;
}
Output
// Write a program multi-level inheritance
#include<iostream>
using namespace std;
class Vehicle{
public:
void Name1(){
cout << "hello world\n";
};
class fourwheeler : public Vehicle{
public:
void Name2(){
cout << "hello guys\n";
};
class Car : public fourwheeler{
public:
void Name3(){
cout << "hello everyone\n";
}
};
int main()
Car obj;
obj.Name3();
obj.Name2();
obj.Name1();
return 0;
}
Output
// Write a program hybrid inheritance
#include<iostream>
using namespace std;
class animal{
public:
animal(){
cout<<"this is an animal ";
};
class cat: public animal{
public:
cat(){
cout<<"this is an cat ";
};
class pet{
public:
pet(){
cout<<"and pet";
};
class kitty: public cat, public pet{
public:
kitty(){
cout<<"\nName of the cat is kitty!\n";
};
int main()
kitty mycat;
return 0;
}
Output
// Write a program to illustrates the use of pointers to objects
#include<iostream>
using namespace std;
class box{
public:
box(double l = 2.0, double b = 2.0, double h = 2.0)
cout <<"Constructor called." <<endl;
length = l;
breadth = b;
height = h;
double volume(){
return length * breadth * height;
private:
double length;
double breadth;
double height;
};
int main()
box box1(3.3, 1.2, 1.5);
box box2(8.5, 6.0, 2.0);
box *ptrbox;
ptrbox = &box1;
cout<<"volume of box1: "<<ptrbox->volume() <<endl;
ptrbox = &box2;
cout<<"volume of box2: "<<ptrbox->volume() <<endl;
return 0;
}
Output
// Write a program virtual function
#include<iostream>
using namespace std;
class Base{
public:
virtual void print(){
cout <<"Base function" << endl;
};
class Derived: public Base{
public:
void print(){
cout <<"derived function" << endl;
};
int main()
Derived derived1;
Base * base = &derived1;
base -> print();
return 0;
}
Output
//Write a program working with single-file
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
int main()
clrscr();
ofstream outf("ITEM");
cout<<"Enter iteam name: ";
char name [30];
cin>>name;
outf<<name<<"\n";
cout<<"Enter item cost:";
float cost;
cin>>cost;
outf<<cost<<"\n";
outf.close();
ifstream inf("ITEM");
inf>>name;
inf>>cost;
cout<<"Item name:"<<name<<"\n";
cout<<"Item cost:"<<cost<<"\n";
inf.close();
getch();
return 0;
}
Output
// Write a program working with multi-files
#include<iostream>
#include<fstream>
int main()
ofstream fout;
fpot.open("Country");
fout<<"United States of America\n";
fout<<"United Kingdom\n";
fout<<"South Korea\n";
fout.close();
fout.open("Captial");
fout<<"Washington\n";
fout<<"London\n";
fout<<"Seoul\n";
const int N = 80;
char line[N];
ifstream fin;
fin.open("Country");
cout<<"Contents of country files\n";
while(fin)
fin.getline(line, N);
cout<<line;
fin.close();
return 0;
}
Output