C++ Assignment (Class and Object)
C++ Assignment (Class and Object)
No : 190301120001
public:
void read(){
cout<<"Enter your Name : ";
cin>>name;
cout<<"\nEnter your admission number : ";
cin>>adm_no;
cout<<"\nEnter your branch : ";
cin>>branch;
cout<<"\nEnter your semester : ";
cin>>sem;
}
void display(){
cout << "Name : "<<name<<"\nAdmission No : "<<adm_no<<"\nBranch : "<<branch<<"\
nSemester : "<<sem<<endl ;
}
};
int main()
{
190301120001||Sohom Ghorai 1
stud s;
s.read();
cout<<"\n\nStudent Details\n"<<endl;
s.display();
return 0;
}
Output :
Enter your Name : Sohom
Student Details
Name : Sohom
Admission No : 1
Branch : CSE
Semester : 3rd
2. Write a program to create a class for student- read n student details and display it.
Code :
#include<iostream>
using namespace std;
class stu{
private:
char name[30];
190301120001||Sohom Ghorai 2
int adm;
char branch[20];
float c;
public :
void read(){
cout<<"Enter your name : ";
cin>>name;
cout<<"\nEnter your admission number : ";
cin>>adm;
cout<<"\nEnter your branch : ";
cin>>branch;
cout<<"\nEnter your CGPA : ";
cin>>c;
}
void print(){
cout<<"Name : "<<name<<"\nAdmission number : "<<adm<<"\nBranch : "<<branch<<"\
nCGPA : "<<c<<endl;
}
};
int main()
{
stu s[100];
int n,i;
for(i=0;i<n;i++){
cout << "Enter Details of students "<< i+1 <<":\n";
s[i].read();
}
for(i=0;i<n;i++){
cout << "\nDetails of student "<<i+1<<":\n";
190301120001||Sohom Ghorai 3
s[i].print();
}
return 0;
}
Output :
Enter number of students : 2
Enter Details of students 1:
Enter your name : Sohom
Enter your admission number : 001
Enter your branch : CSE
Enter your CGPA : 8.2
Enter Details of students 2:
Enter your name : Rik
Enter your admission number : 002
Enter your branch : ME
Enter your CGPA : 7.8
Details of student 1:
Name : Sohom
Admission number : 1
Branch : CSE
CGPA : 8.2
Details of student 2:
Name : Rik
Admission number : 2
Branch : ME
CGPA : 7.8
3.Write a program to create a class for student- read n student details and display the student
datails whose percentage is grater than 80.
Code :
190301120001||Sohom Ghorai 4
#include <iostream>
char name[100];
int id;
int age;
float percentage;
public:
void read(){
190301120001||Sohom Ghorai 5
cin >> age;
check(percentage);
void disp()
{
cout << "id : " << id<< endl;
cout << " name : " << name << endl;
cout << "salary : " << percentage << endl;
cout << "age : " << age<< endl;
}
};
int main ()
{
student s[100];
int n,i;
cout << "Enter number of students : ";
cin >> n;
for(i=0;i<n;i++){
cout << "Enter Details of students "<< i+1 <<":\n";
s[i].read();
}
return 0;
}
Output :
Enter number of students : 2
Enter Details of students 1:
enter student details:
student id : 1
name : Sohom
percentage: 88
age: 19
id : 1
name : Sohom
salary : 88
age : 19
190301120001||Sohom Ghorai 6
Enter Details of students 2:
enter student details:
student id : 2
name : Rik
percentage: 76
age: 19
Condition is not satisfied
4.Write a program to create a class for Employee (Name, Id, Salary, age )- read n employee
details and display the employee datails whose salary greater than 30000 or age greater than
35.
Code :
#include <iostream>
#include<conio.h>
using namespace std;
class Employee
{
private:
char name[100];
int id;
int age;
float salary;
190301120001||Sohom Ghorai 7
cout<<"These conditions {Salary>=30000 or age >= 35} are not satisfied";
}
}
public:
void take()
{
cout << "enter employee details:";
cout << "\nemployee id : ";
cin >> id;
cout << "name : ";
cin >> name;
cout << "salary : ";
cin >> salary;
cout << "age: ";
cin >> age;
check(salary,age);
void disp()
{
cout << "\nid : " << id<< endl;
cout << "name : " << name << endl;
cout << "salary : " << salary << endl;
cout << "age : " << age<< endl;
}
};
int main()
{
Employee e[100];
190301120001||Sohom Ghorai 8
int n,i;
cout << "Enter number of employees : ";
cin >> n;
for(i=0;i<n;i++){
cout << "Enter Details of Employee "<< i+1 <<":\n";
e[i].take();
}
getch();
return 0;
}
Output :
Enter number of employees : 2
Enter Details of Employee 1:
enter employee details:
employee id : 1
name : Sanjay
salary : 36000
age: 37
id : 1
name : Sanjay
salary : 36000
age : 37
Enter Details of Employee 2:
enter employee details:
employee id : 2
name : Sachin
salary : 28000
age: 26
These conditions {Salary>=30000 or age >= 35} are not satisfied
190301120001||Sohom Ghorai 9
1. Define a class student with the following specification
Private members of class student
admno integer
sname 20 character
eng. math, science float
total float
ctotal() a function to calculate eng + math + science with float return type.
Public member function of class student
Takedata() Function to accept values for admno, sname, eng, science and invoke
ctotal() to calculate total.
Showdata() Function to display all the data members on the screen.
Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std ;
class stud{
private :
int admno;
char sname[20];
float eng,math,science;
float total;
float ctotal()
{
return eng+math+science;
}
public :
void rd(){
cout<<"Enter Admission no : ";
cin >> admno;
cout<<"Enter name :";
cin>>sname;
cout<<"Enter English , Math and Science Marks : ";
cin>>eng>>math>>science;
190301120001||Sohom Ghorai 10
total = ctotal();
}
void pd(){
cout<<"Admission No: "<< admno <<" Name: "<< sname<<" English Marks :" <<eng<<"
Math Marks:" <<math<<" Science Marks : "<<science<<"\nTotal = "<<total;
}
};
int main()
{
stud s;
s.rd();
s.pd();
getch();
return 0;
}
Output :
Enter Admission no : 1
Enter name :Sohom
Enter English , Math and Science Marks : 87
81
88
Admission No: 1 Name: Sohom English Marks :87 Math Marks:81 Science Marks : 88
Total = 256
2. 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
190301120001||Sohom Ghorai 11
invoke the function calcavg()
displaydata() Function to display the data members on the screen.
Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class batsman{
int bcode;
char bname[20];
int innings,notout,runs;
int batavg;
void calcavg()
batavg=runs/(innings-notout);
public :
cin>> bcode;
cin>>bname;
190301120001||Sohom Ghorai 12
cin>>innings>>notout>>runs;
calcavg();
void displaydata(){
};
int main()
batsman b;
b.readdata();
b.displaydata();
getch();
return 0;
Output :
190301120001||Sohom Ghorai 13
Enter batsman code : 7
Enter batsman name : MSD
enter innings ,notout and runs : 350
80
10773
Batsman code : 7
Batsman name : MSD
Innings : 350
Not out :80
Runs :10773
Batting Average :39
3. Define a class TEST in C++ with following description:
Private Members
TestCode of type integer
Description of type string
NoCandidate of type integer
CenterReqd (number of centers required) of type integer
A member function CALCNTR() to calculate and return the number of centers as
(NoCandidates/100+1)
Public Members
- A function SCHEDULE() to allow user to enter values for TestCode, Description,
NoCandidate & call function CALCNTR() to calculate the number of Centres
- A function DISPTEST() to allow user to view the content of all the data members
Code :
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class TEST{
private:
int TestCode;
char Description[30];
190301120001||Sohom Ghorai 14
int NoCandidate;
int CenterReqd;
int CALCNTR()
return NoCandidate/100+1;
public:
void SCHDULE(){
cin>> TestCode;
cin>>Description;
cin>>NoCandidate;
CenterReqd=CALCNTR();
void DISPTEST(){
190301120001||Sohom Ghorai 15
};
int main ()
TEST t;
t.SCHDULE();
t.DISPTEST();
getch();
return 0;
}
Output :
Enter Test code : 123
Enter description : C++
Enter no of candidates : 120
Test code 123
Descripton C++
No of candidate 120
Center required 2
190301120001||Sohom Ghorai 16
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
Code :
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class FLIGHT
{ int Fno;
char Destination[20];
float Distance, Fuel;
void CALFUEL()
{
if (Distance<=1000)
Fuel=500;
else if (Distance> 1000 && Distance<=2000)
Fuel=1100;
else if(Distance>2000)
Fuel=2200;
}
public:
void FEEDINFO(){
cout<<"Flight No : ";
cin>>Fno;
cout<<"Destination : ";
cin>>Destination;
cout<<"Distance : ";
cin>>Distance;
CALFUEL();
}
void SHOWINFO(){
cout<<"Flight No : "<<Fno<<endl;
cout<<"Destination : "<<Destination<<endl;
cout<<"Distance : "<<Distance<<endl;;
190301120001||Sohom Ghorai 17
cout<<"Fuel : "<<Fuel<<endl;;
}
};
int main()
{ FLIGHT F;
F.FEEDINFO();
F.SHOWINFO();
getch();
return 0;
}
Output :
Flight No : 100
Destination : Kolkata
Distance : 450
Flight No : 100
Destination : Kolkata
Distance : 450
Fuel : 500
5. 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.
Code :
#include<iostream>
#include<stdio.h>
#include<conio.h>
190301120001||Sohom Ghorai 18
using namespace std;
class BOOK{
int BOOKNO;
char BOOKTITLE[20];
float PRICE;
void TOTAL_COST(int N)
float tcost;
tcost=PRICE*N;
cout<<tcost;
public:
void INPUT()
cin>>BOOKNO;
cin>>BOOKTITLE;
cin>>PRICE;
190301120001||Sohom Ghorai 19
}
void PURCHASE()
int n;
cin>>n;
TOTAL_COST(n);
};
int main()
BOOK b;
b.INPUT();
b.PURCHASE();
getch();
return 0;
}
Output :
190301120001||Sohom Ghorai 20
Enter Book Number : 12345
Enter Book Title : Sherlock
Enter price per copy : 450
Enter number of copies to purchase : 20
Total cost is : 9000
Code :
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
class REPORT
int adno;
char name[20];
float marks[5];
190301120001||Sohom Ghorai 21
float average;
void GETAVG()
average = (marks[0]+marks[1]+marks[2]+marks[3]+marks[4])/5;
public:
void READINFO();
void DISPLAYINFO();
};
void REPORT::READINFO()
do
cin>>adno;
}while(adno<999 || adno>10000);
190301120001||Sohom Ghorai 22
cout<<"Enter name : ";
cin>>name;
for(int i=0;i<5;i++)
cout<<"Subject "<<i+1<<":";
cin>>marks[i];
};
GETAVG();
void REPORT::DISPLAYINFO()
190301120001||Sohom Ghorai 23
cout<<"Admission number : "<<adno<<" Name : "<<name<<" Marks are : "<< marks[0]<<" "<< marks[1]
int main()
REPORT r;
r.READINFO();
r.DISPLAYINFO();
getch();
return 0;
Output :
Enter Book Number : 12345
Enter Book Title : Sherlock
Enter price per copy : 450
Enter number of copies to purchase : 20
Total cost is : 9000
190301120001||Sohom Ghorai 24