0% found this document useful (0 votes)
22 views

Programming Assignments For Ooc2

Uploaded by

pradeepsns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Programming Assignments For Ooc2

Uploaded by

pradeepsns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming Assignments for OOC

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( );

}; // End of Class Student

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( );
}

2) Write a C++ Program to read and display the information of N student

#include<iostream.h>
using namespace std;

class student
{
private:

char name[20],regd[10],branch[10];
int sem;
public:
void input( );
void display( );

}; // End of Class Student

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( );
}

 Similarly you can read and display the information about N


Employee
 You can read and display the information about N Books

3) Write a C++ program to find area of triangle, circle,and rectangle using


function overloading.
Note: You all know that Function Overloading means functions are having same name
and differ in the signatures (Number of parameters, Types of parameters and
sequence of parameters)

#include<iostream.h>
#include<conio.h>
const float pi=3.14;

float area(float b,float h) // This Function calculates area of Triangle


{
float ar;
ar=0.5*b*h;
return ar;
}
float area(float r) // This Function calculates area of Circle (Overloaded Function)
{
float ar;
ar=pi*r*r;
return ar;
}
float area(float l,float b) // This Function calculates area of Rectangle (Overloaded Function)
{
float ar;
ar=l*b;
return ar;
}
void main( )
{
float b,h,r,l;
float result;
clrscr( );
cout<<“\nEnter the Base & Hieght of Triangle: \n”;
cin>>b>>h;
result=area(b,h);
cout<<“\nArea of Triangle: “<<result<<endl;
cout<<“\nEnter the Radius of Circle: \n”;
cin>>r;
result=area(r);
cout<<“\nArea of Circle: “<<result<<endl;
cout<<“\nEnter the Length & Bredth of Rectangle: \n”;
cin>>l>>b;
result=area(l,b);
cout<<“\nArea of Rectangle: “<<result<<endl;
getch();
}

4 ) Define a class batsman with the following specifications:


Private members:
bcode 4 digits code number
bname 20 characters
innings, notout, runs integer type
batavg it is calculated according to the formula –
batavg =runs/(innings-notout)
calcavg() Function to compute batavg

Public members:
readdata() Function to accept value from bcode, name, innings, notout and

invoke the function calcavg()


displaydata() Function to display the data members on the screen.

#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( );
}

Define a class BOOK with the following specifications :


Private members of the class BOOK are
BOOK NO integer type
BOOKTITLE 20 characters
PRICE float (price per copy)
TOTAL_COST() A function to calculate the total cost for N number of copies where N is

passed to the function as argument.

Public members of the class BOOK are


INPUT( ) function to read BOOK_NO. BOOKTITLE, PRICE
PURCHASE() function to ask the user to input the number of copies to be purchased.
It invokes TOTAL_COST( ) and prints the total cost to be paid by the user.
Note : You are also required to give detailed function definitions.
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

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 complex::add(complex c1,complex c2)


{
real=c1.real+c2.real;
img=c1.img+c2.img;
}

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( );
}

Write a C++ program to count number of objects

#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 item :: count; //count is initialized to zero

int main()
{
item a, b, c;
a.getcount();
b.getcount();
c.getcount();

a.getdata(100);
b.getdata(200);
c.getdata(300);

cout<<"After reading data"<<endl;

a.getcount();
b.getcount();
c.getcount();
}

You might also like