In this article Simple Programs Using Class we give the simple programs using class with output such as area of rectangle, swapping two numbers, simple interest etc.
Simple Programs Using Class
// 1. To Find Area of Rectangle Using Class
#include<iostream.h>
#include<conio.h>
class Rect_Class
{
private:
int l,b; // data members
public:
void getdata(); // member functions
void show_area();
};
void Rect_Class::getdata()
{
cout<<“\n\n Enter the Length and Breadth: ” ;
cin>>l>>b;
}
void Rect_Class::show_area()
{
cout<<“\n\n Area of Rectangle: “<<l*b;
}
void main()
{
Rect_Class obj;
clrscr();
obj.getdata();
obj.show_area();
getch();
}
Output:
Enter the Length and Breadth: 10
5
Area of Rectangle: 50
// 2. Swapping of two numbers using class
#include<iostream.h>
#include<conio.h>
class Swap_Class
{
private:
int x,y;
public:
void getdata()
{
cout<<“\n\n Enter the two numbers: “;
cin>>x>>y;
}
void show();
};
void Swap_Class::show()
{
int temp;
cout<<“\n\n Before Swap: \n”<<x<<“\t”<<y;
temp=x;
x=y;
y=temp;
cout<<“\n\n After Swap: \n”<<x<<“\t”<<y;
}
void main()
{
Swap_Class p;
clrscr();
p.getdata();
p.show();
getch();
}
Output:
Enter the two numbers: 45
55
Before Swap:
45 55
After Swap:
55 45
// 3. To find Perimeter of rectangle using class
#include<iostream.h>
#include<conio.h>
class Rect_Peri
{
private:
int l,b;
public:
void getdata()
{
cout<<“\n\n Enter the length and Breadth: “;
cin>>l>>b;
}
void show();
}ob;
void Rect_Peri::show()
{
cout<<“\n\n Perimeter of Rectangle: “<<2*(l+b);
}
void main()
{
clrscr();
ob.getdata();
ob.show();
getch();
}
Output:
Enter the length and Breadth: 5
4
Perimeter of Rectangle: 18
// 4. To find Volume of Cube using class
#include”iostream.h”
#include”conio.h”
class Cube_Class
{
private:
long double s; // data member
public:
void getdata() // member function
{
cout<<“\n\n Enter the side value: “;
cin>>s;
}
void show();
};
void Cube_Class::show()
{
cout<<“\n\n Volume of Cube: “<<s*s*s;
}
void main()
{
Cube_Class p;
clrscr();
p.getdata();
p.show();
getch();
}
Output:
Enter the side value: 5
Volume of Cube: 125
// 5. To find simple interest using class
#include<iostream.h>
#include<conio.h>
class SI_Class
{
private:
long double p,r,y; // data members
public:
void getdata() // member function
{
cout<<“\n\n Enter the principle amount: “;
cin>>p;
cout<<“\n Enter the rate: “;
cin>>r;
cout<<“\n Enter number of year: “;
cin>>y;
}
void show();
}obj;
void SI_Class::show()
{
long double si=(p*r*y)/100;
cout<<“\n\n Simple Interest: “<<si;
}
void main()
{
clrscr();
obj.getdata();
obj.show();
getch();
}
Output:
Enter the principle amount:
100000
Enter the rate:
10
Enter number of year:
5
Simple Interest: 50000
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining