Programming Assignments For Ooc2
Programming Assignments For Ooc2
1) Write a C++ Program to read and display the information of one student
#include<iostream.h>
using namespace std;
class student
{
private:
char name[20],regd[10],branch[10];
int sem;
public:
void input( );
void display( );
void student::input( )
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Regdno.:";
cin>>regd;
cout<<"Enter Branch:";
cin>>branch;
cout<<"Enter Sem:";
cin>>sem;
}
void student::display( )
{
cout<<"\nName:"<<name;
cout<<"\nRegdno.:"<<regd;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
}
void main( )
{
student s; // Create the object of class Student (i.e. s), By using that object we can access any
methods
s.input( );
s.display( );
}
#include<iostream.h>
using namespace std;
class student
{
private:
char name[20],regd[10],branch[10];
int sem;
public:
void input( );
void display( );
void student::input( )
{
cout<<"Enter Name:";
cin>>name;
cout<<"Enter Regdno.:";
cin>>regd;
cout<<"Enter Branch:";
cin>>branch;
cout<<"Enter Sem:";
cin>>sem;
}
void student::display( )
{
cout<<"\nName:"<<name;
cout<<"\nRegdno.:"<<regd;
cout<<"\nBranch:"<<branch;
cout<<"\nSem:"<<sem;
}
void main( )
{
student s[100]; // Create the Array object of 100 student class Student (i.e. s), By using that
// object we can access any methods
int n;
cout << "Enter total number of students: ";
cin >> n;
for(int i=0;i< n; i++)
{
cout << "Enter details of student " << i+1 << ":\n";
s[i].input( );
}
cout << endl;
for(int i=0;i< n; i++)
{
cout << "Details of student " << (i+1) << ":\n";
s[i].display( );
}
#include<iostream.h>
#include<conio.h>
const float pi=3.14;
Public members:
readdata() Function to accept value from bcode, name, innings, notout and
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class batsman
{
int bcode;
char bname[20];
int innings,notout,runs;
int batavg;
void calcavg()
{
batavg=runs/(innings-notout);
}
public :
void readdata ();
void displaydata();
};
void batsman::readdata ()
{
cout<<"Enter batsman code ";
cin>> bcode;
cout<<"Enter batsman name ";
gets(bname);
cout<<"enter innings,notout and runs ";
cin>>innings>>notout>>runs;
calcavg();
}
void batsman::displaydata()
{
cout<<"Batsman code "<<bcode<<"\nBatsman name "<<bname<<"\nInnings "<<innings
<<"\nNot out "<<notout<<"\nRuns "<<runs<<"\nBatting Average "<<batavg;
}
void main()
{
batsman obj; // object of class batsman
obj.readdata();
obj.displaydata();
getch( )
}
5) Define a class in C++ with following description:
Private Members
A data member Flight number of type integer
A data member Destination of type string
A data member Distance of type float
A data member Fuel of type float
A member function CALFUEL() to calculate the value of Fuel as per the following criteria
Distance Fuel
<=1000 500
more than 1000 and <=2000 1100
more than 2000 2200
Public Members
A function FEEDINFO() to allow user to enter values for Flight Number, Destination, Distance & call function
CALFUEL() to calculate the quantity of Fuel
A function SHOWINFO() to allow user to view the content of all the data members
#include<iostream.h>
#include<conio.h>
class TEST
{
private:
int TestCode;
char Description[30];
int NoCandidate;
int CenterReqd;
int CALCNTR( )
{
return NoCandidate/100+1;
}
public:
void SCHDULE( );
void DISPTEST( );
};
void TEST::SCHDULE( )
{
cout<<"Enter Test code ";
cin>> TestCode;
cout<<"Enter description ";
gets(Description);
cout<< "Enter no of candidates ";
cin>>NoCandidate;
CenterReqd=CALCNTR( );
}
void TEST :: DISPTEST( )
{
cout<<"Test code "<<TestCode<<"\nDescripton "<<Description<<"\nNo of candidate "
<<NoCandidate<<"\nCenter required "<<CenterReqd;
}
void main ()
{
TEST obj;
obj.SCHDULE( );
obj.DISPTEST( );
getch( );
}
class BOOK
{
int BOOKNO;
char BOOKTITLE[20];
float PRICE;
void TOTAL_COST(int N)
{
float tcost;
tcost=PRICE*N;
cout<<tcost;
}
public:
void INPUT( )
{
cout<<"Enter Book Number ";
cin>>BOOKNO;
cout<<"Enter Book Title ";
gets(BOOKTITLE);
cout<<"Enter price per copy ";
cin>>PRICE;
}
void PURCHASE( )
{
int n;
cout<<"Enter number of copies to purchase ";
cin>>n;
cout<<"Total cost is ";
TOTAL_COST(n);
}
};
int main( )
{
BOOK obj;
obj.INPUT( );
obj.PURCHASE( );
getch( );
return 0;
}
Write a C++ program to define class complex with real and imaginary data as a members and
getdata( ),add( ) and display_data( ) as member function to read, add and display complex object.
#include<iostream.h>
#include<conio.h>
class complex
{
int real;
int img;
public:
void getdata( );
void display_data( );
void add(complex c1,complex c2);
};
void complex::getdata()
{
cout<<"Enter the real and img part";
cin>>real>>img;
}
void complex::display_data()
{
cout<<"\n"<<real<<"+"<<img<<"i"<<"\n";
}
void main()
{
clrscr();
complex c1,c2,c3;
c1.getdata();
c2.getdata();
c3.add(c1,c2);
cout<<"The complex number of c1:";
c1.display_data( );
cout<<"The complex number of c2:";
c2.display_data( );
cout<<"The result is:";
c3.display_data( );
}
#include<iostream>
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count++;
}
void getcount()
{
cout<<"Count = "<<count<<endl;
}
};
int main()
{
item a, b, c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
}