C++ Program Samples / Examples


1. Write a program to perform arithmetic operations using objects and classes.

 #include<iostream.h>

#include<conio.h>

class arithmetic

{

int a,b;

public:

void get()

{

cout<<”Enter the value for A and B”<<endl;

cin>>a>>b;

}

void put()

{

cout<<”\n——————-”<<endl;

cout<<”Arithmetic Operation”<<endl;

cout<<”——————-”<<endl;

cout<<”\nAddition:”<<a+b<<endl;

cout<<”\nDifference:”<<a-b<<endl;

cout<<”\nProduct:”<<a*b<<endl;

cout<<”\nDivision:”<<a/b<<endl;

cout<<”\nModule:”<<a%b<<endl;

}

};

void main()

{

arithmetic maths;

clrscr();

maths.get();

maths.put();

getch();

}

2. Write a program to find volume of cube, cylinder, rectangular box using function overloading.

 

#include<iostream.h>

#include<conio.h>

class function

{

public:

int volume(int);

double volume(double, int);

long volume(long, int, int);

};

int function::volume(int s)

{

return (s*s*s);

}

double function::volume(double r,int h)

{

return(3.14519*r*r*h);

}

long function::volume(long l,int b,int h)

{

return(l*b*h);

}

int main()

{

function f;

clrscr();

cout<<”volume of Cube:—>\t”<<f.volume(10)<<endl;

cout<<”volume of Cylinder:—>\t”<<f.volume(2.5,8)<<endl;

cout<<”volume of Rectangle:—>”<<f.volume(100L,75,15)<<endl;

getch();

return 0;

}

3. Define a class Competition in C++ with the following descriptions;

    Data Members:

            Event_no                    integer

            Description                 char(30)

            Score                           integer

            Qualified                    char

    Member function:

  • A constructor to assign initial values Event_No number as 101.

Description as “State level”, Score is 50 and qualified as ‘N’.

  • Input ( ), To take the input for event_no, description and score.
  • Award (int), To award qualified as “Y”, if score is more than the

Cutoffscore passed as argument to the function else ‘N’.

  • Show( ), To display all the details.

 #include<iostream.h>

#include<conio.h>

#include<string.h>

class competition

{

protected:

int event_no,score;

char desc[30],qual;

public:

competition()

{

event_no=101;

// desc=”StateLevel”;

strcpy(desc,”statelevel” );

score=50;

qual=’N’;

}

void input()

{

cout<<”Enter the Event_no, Description and Score”<<endl;

cin>>event_no>>desc>>score;

}

void award(int cutoff)

{  if(cutoff<score)

qual=’Y’;

}

void display()

{

cout<<”\nCompetition Details”<<endl;

cout<<”Event_no:”<<event_no<<endl

<<”Description:”<<desc<<endl

<<”Score:”<<score<<endl

<<”Qualified:\n”<<qual<<endl;

}

};

void main()

{

competition c[10];

int num,cutoff;

clrscr();

cout<<” \nEnter the number of members”<<endl;

cin>>num;

cout<<” \nEnter the cutoff score”<<endl;

cin>>cutoff;

for(int i=0;i<num;i++)

c[i].display();

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

{

c[i].input();

c[i].award(cutoff);

}

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

c[i].display();

getch();

}

 

4. Write a C++ program to concatenate & two strings using operator overloading.


 

5. Write a C++ program to demonstrate virtual function.

 #include<iostream.h>

#include<conio.h>

class Base

{

public:

virtual void display()

{

cout<<”This is the Base class virtual fuction”<<endl;

}

};

class Derived1:public Base

{

public:

void display()

{

cout<<”This is the Derived1 class display function”<<endl;

}

};

class Derived2:public Base

{

public:

void display()

{

cout<<”This is the Derived2 class display function”<<endl;

}

};

void main()

{

Base *b;

b=new Base;

b->display();   //displays the base class display function

b=new Derived1;

b->display();   //displays the derived1 class display function

b=new Derived2;

b->display();   //displays the derived2 class display function

getch();

}

6. Write a C++ program to create a base class ‘student’, from this inherit a new called ‘Exams’, containing marks1, marks2, marks3 as its data members. Also create another base class called ‘sports’ having aport_grade as its data member. Now, create another class but a derived class of ‘Exams’ and ‘Sports’ classes and call it as ‘awards’. Use the suitable member functions in all the classes. You can use the name, roll number and semester as the member of the base class.

 #include<iostream.h>

#include<conio.h>

class student

{

char name[30];

int roll,sem;

public:

void insert()

{

cout<<”Enter the Student Name,Roll number,Semister(1-8)”<<endl;

cin>>name>>roll>>sem;

}

void display()

{

cout<<”\n\n\n*-*-*-*-* Student Details *-*-*-*-*”<<endl;

cout<<”\nName:—>”<<name<<endl

<<”\nRoll num:—>”<<roll<<endl

<<”\nSemister:—>”<<sem<<endl;

}

};

class exam:public student

{

int m1,m2,m3;

public:

void get()

{

insert();

cout<<”Enter the 3 subject marks”<<endl;

cin>>m1>>m2>>m3;

}

void put()

{

display();

int total;

total=m1+m2+m3;

cout<<”\n\n\tMarks1:”<<m1<<endl

<<”\n\tMarks2:”<<m2<<endl

<<”\n\tMarks3:”<<m3<<endl

<<”\t————”<<endl

<<”\tTotal :”<<total<<endl;

}

};

class sports //2nd Base class

{

public:

char sports_grade[20]; //data member

};

class awards:public exam,sports

{

public:

void input()

{

put();

cout<<”\n\nGrades to Match”<<endl

<<”*-*-*-*-*-*-*-*-*”<<endl

<<” \nA Grade – 200 to 300 “<<endl

<<” \nB Grade – 100 to 199 “<<endl

<<” \nC Grade – 0 to 99 “<<endl;

}

};

void main()

{

awards obj;

clrscr();

obj.get();

obj.input();

getch();

}

7. Write a C++ program to declare result of a student using multi level inheritance.

 #include<iostream.h>

#include<conio.h>

class student

{

char name[20];

int id;

public:

void get();

void put();

};

class subjects:public student

{

protected:

int sub1,sub2,sub3;

public:

void get();

void put();

};

class result:public subjects

{

public:

void final();

void display();

};

void student::get()

{

cout<<”Enter the Student Name and ID number\n”<<endl;

cin>>name>>id;

}

void student::put()

{

cout<<”Name:—>”<<name<<endl<<”ID number:—>”<<id<<endl;

}

void subjects::get()

{

student::get();

cout<<”Enter the marks for 3 subjects”<<endl;

cin>>sub1>>sub2>>sub3;

}

void subjects::put()

{

student::put();

cout<<”Marks of subject1:—>”<<sub1<<endl

<<”Marks of subject2:—>”<<sub2<<endl

<<”Marks of subject3:—>”<<sub3<<endl;

}

void result::final()

{

subjects::get();

}

void result::display()

{

int total;

subjects::put();

total=(sub1+sub2+sub3);

cout<<”Total marks:”<<total<<endl;

if (total<=120)

cout<<”Student Failed”<<endl;

else

cout<<”Student Passed”<<endl;

}

void main()

{

result score;

clrscr();

cout<<”*******Student Results*******”<<endl;

score.final();

score.display();

getch();

}

8. Write a program in C++ to add Co-ordinates of plain. The class contains X, Y coordinates. Create Three objects. Use a constructor to pass one pair of coordinates and a function to accept the Second pair. Add these variables of two objects and store result in the third object.

 

 

 

9. Write a program in C++ to accept details of N Employees. Generate the employee number for them using the static variable and static functions.

 #include<iostream.h>

#include<conio.h>

class emp

{

static int eid;

char name[20],desig[20];

public:

static void loop()

{

eid++;

cout<<eid<<endl;

}

void input()

{

cout<<”Enter the employee Name designation”<<endl;

cin>>name>>desig;

}

void output()

{

cout<<”\nName:”<<name<<endl

<<”Designation:\n”<<desig<<endl;

}

};

int emp::eid=100;

void main()

{

emp e[10];

clrscr();

int n;

cout<<”Enter the max no.”<<endl;

cin>>n;

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

{

e[i].input();

}

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

{

e[i].output();

emp::loop();

}

getch();

}

10. Write a C++ program to define a class ‘box’ with length, breadth, and heighr as

data members, and inputvalue(), printvalue() and volume() as member functions.

 #include<iostream.h>

#include<conio.h>

class box

{

float length,breadth,height,formula;

public:

void inputvalue()

{

cout<<”Enter the value for length,breadth & heigth”<<endl;

cin>>length>>breadth>>height;

}

void volume()

{

formula=length*breadth*height;

}

void printvalue()

{

cout<<”Formula:—>”<<formula<<endl;

}

};

void main()

{

box b;

clrscr();

b.inputvalue();

b.volume();

b.printvalue();

getch();

}

 

11.Wtite a C++ program to define a class called customer with account number, name, address as data member; deposite(), withdraw () and balance() as member functions consider the banking transactions carefully.

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class customer

{

char name[20],addr[20];

float acc,balance;

public:

customer()

{

cout<<”Enter your Name, Address and Account no.”<<endl;

cin>>name>>addr>>acc;

cout<<”Enter the balance amount”<<endl;

cin>>balance;

}

void deposit()

{

float amount;

cout<<”Enter the deposite maount”<<endl;

cin>>amount;

balance=balance+amount;

cout<<”The current balance=”<<balance<<endl;

}

void withdraw()

{

float amount;

cout<<”Enter the  amount to withdraw”<<endl;

cin>>amount;

if (amount <=balance)

{

balance=balance-amount;

cout<<”Amount drawn=”<<amount<<endl;

cout<<”Current balance=”<<balance<<endl;

}

else

cout<<”your amount is more that ur balance”<<endl;

}

void getbalance()

{

cout<<”the current balance=”<<balance<<endl;

}

void menu()

{

cout<<”———-”<<endl;

cout<<”D—->Deposit”<<endl;

cout<<”W—->Withdraw”<<endl;

cout<<”G—->Get balance”<<endl;

cout<<”Q—->Quit”<<endl;

cout<<”Enter your option please”<<endl;

}

void quit()

{

exit(0);

}

};

void main()

{

char ch;

customer acc;

clrscr();

while(1)

{

acc.menu();

cin>>ch;

switch(ch)

{

case ‘D’:

case ‘d’:

acc.deposit();

break;

case ‘W’:

case ‘w’:

acc.withdraw();

break;

case ‘G’:

case ‘g’:

acc.getbalance();

break;

case ‘Q’:

case ‘q’:

acc.quit();

break;

default:

cout<<”Invalid operation”<<endl;

break;

}

getch();

}

}

12. Write a C++ program to create an object of a class called ‘employee’ containing the employee code, name, designation, basic salary, HRA, DA, gross salary as data members. Process 10 such objects using constructors and destructors.

 #include<iostream.h>

#include<conio.h>

#include<string.h>

int count=0;

class employee

{

long int code, sal,hra,da,total;

char name[20],des[20];

public:

employee(int d,char n[20],char di[20],int s)

{

count++;

code=d;

strcpy(name,n);

strcpy(des,di);

sal=s;

}

~employee()

{

cout<<”\n”<<count<<endl;

hra=((sal*10)/100);

da=((sal*5)/100);

total=sal+hra+da;

cout<<”*-*-*-*-*-*-*-*-*Employee Details*-*-*-*-*-*-*-*”<<endl

<<”Code no.=”<<code<<endl

<<”Name:”<<name<<endl

<<”Designation:”<<des<<endl

<<”Basic Salary:”<<sal<<endl

<<”HRA:”<<hra<<endl

<<”DA:”<<da<<endl

<<”Gross Salary:”<<total<<endl<<endl;

count–;

cout<<count<<”Destroyed objects\n”<<endl;

}

};

void main()

{

clrscr();

employee e1(100,”Rahul”,”Team Leader”,1000),

e2(110,”Rakesh”,”Manager”,2000),

e3(120,”Rohan”,”Clerk”,3500),

e4(130,”Suhani”,”Project engg”,1000),

e5(140,”Gowri”,”Bluecollar”,1500),

e6(150,”Mambo”,”Officeboy”,1700),

e7(160,”Tom”,”S/w engg”,6000),

e8(170,”Susan”,”Engg”,20000),

e9(180,”Gaana”,”CEO”,30000),

e10(190,”Sam”,”Tester”,15000);

getch();

}

I know well about i didn’t answer for some of question. If possible, answer me for those unfinished questions.

Thanks in advance who gonna answer.

Happy Coding… :)

About these ads

Tagged: , , , , , ,

One thought on “C++ Program Samples / Examples

  1. homecleaning January 25, 2013 at 11:10 am Reply

    I like what you guys are up also. Such intelligent work and reporting! Carry on the superb works guys I¡¦ve incorporated you guys to my blogroll. I think it’ll improve the value of my website :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: