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

assignment

Uploaded by

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

assignment

Uploaded by

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

KASHAF FEROZ (SS1)

BSCS51F24S012

Question 1 Write a program that declared a class with one integer data
member and two member functions for input data and output data. Decide
appropriate access modifiers for these members. Use this class in your
program

#include <iostream>

using namespace std;

class my class

#include<iostream>

using namespace std ;

class REPORT

private:

int adno;

char name[20];

float marks[5], average;

void GETAVG()

int sum = 0;

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

sum = sum + marks[i];

average = sum/5.0f;

}
public:

void READINFO()

cout<<"Enter id ";

cin>>adno;

cout<<"Enter Name ";

cin>>name;

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

cout<<"Enter marks of subject-"<<i+1<<" ";

cin>>marks[i];

GETAVG ();

void DISPLAYINFO ()

cout<<"admision Id "<<adno<<endl;

cout<<"Name "<<name<<endl;

cout<<"Average "<<average<<endl;

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

cout<<"Marks of subject-"<<i+1<<" is "<<marks[i]<<endl;

};
int main()

REPORT R1;

R1.READINFO();

cout<<endl;

R1.DISPLAYINFO();

return 0;

}int data;

public:

void getdata()

cout << "Enter data: ";

cin >> data;

void showdata()

cout << "data is: " << data << endl;

};

int main()

Myclass obj1, obj2;

obj1.getdata();

obj2.getdata();

obj1.showdata();

obj2.showdata();
return 0;

Question 2 Create a class named Distance that has feets (as int) and inches (as
float). The class has Getdist (int, float) to get the specified value in object,
Showdist () to display distance object in feets’ inches” format. Write main ()
function to create two distance objects. Get the value in two objects and
display all objects.

#include<iostream>

using namespace std ;

class Distance

private:

int feet;

float inches;

public:

void Getdist(int f,float i)

feet=f;

inches=i;
}

void Showdist()

cout<< feet << '\'' << inches << '\"' << endl;

float totalinches()

return (feet*12 + inches);

};

int main()

Distance Dist1,Dist2;

cout<<"Enter feet and inches for distance 1:";

int f1;

float i1;

cin>>f1>>i1;

Dist1.Getdist(f1,i1);

cout<<"Enter feet and inches for distance 2:";

int f2;
float i2;

cin>>f2>>i2;

Dist2.Getdist(f2,i2);

/*float tincesd1 = Dist1.totalinches();

float tincesd2 = Dist2.totalinches();

if(tincesd1 > tincesd2)

cout<<"\nDistance 1:";

Dist1.Showdist();

}*/

if(Dist1.totalinches() > Dist2.totalinches())

cout<<"\nDistance 1:";

Dist1.Showdist();

else

cout<<"Distance 2:";

Dist2.Showdist();

return 0;
}

Question 3 Create a class named TIME that has hours, minutes and seconds data
members as integer. The class has settime (int, int, int) to set the specified value in
object, showtime () to display time object in hh:mm:ss format. Write main ()
function to create two time objects. Set the value in two objects and display all
time objects.
#include <iostream>

using namespace std;

class TIME {

private:

int hours, minutes, seconds;

public:

void settime(int h, int m, int s) {

hours = h;

minutes = m;

seconds = s;

void showtime() {
cout << hours << ":" << minutes << ":" << seconds << endl;

};

int main() {

TIME t1, t2;

t1.settime(10, 30, 45);

t2.settime(12, 15, 20);

cout << "Time 1: ";

t1.showtime();

cout << "Time 2: ";

t2.showtime();

return 0;

Question 4 Create a class Person that has three data members Pid, Pname,
PSalary with appropriate data type. Person class also contains the member
functions: getdata() function is used to input values, showdata() function is
used to display value, setdata() function is used to set the values of data
members using parameters, getSalary() function is used to return the value of
person salary. The program should create three objects of the person class
and input values for these objects. The program display the details of highest
salary holder person.
Q no 4

#include <iostream>

using namespace std;

class Person {

private:

int Pid;

string Pname;

double PSalary;

public:

void getdata() {

cout << "Enter ID, Name, and Salary: ";

cin >> Pid >> Pname >> PSalary;

void showdata() {

cout << "ID: " << Pid << ", Name: " << Pname << ", Salary: " << PSalary << endl;

void setdata(int id, string name, double salary) {

Pid = id;

Pname = name;
PSalary = salary;

double getSalary() {

return PSalary;

};

int main() {

Person persons[3];

for (int i = 0; i < 3; i++) {

persons[i].getdata();

int maxIndex = 0;

for (int i = 1; i < 3; i++) {

if (persons[i].getSalary() > persons[maxIndex].getSalary()) {

maxIndex = i;

cout << "\nDetails of the highest salary holder:\n";

persons[maxIndex].showdata();

return 0;

}
QUESTION 5 Write a class Employee with three data members Eid type int, Ename type string
and Esalary type double. It also contains the following member function:
• The get() function is used to input values
• The show() function is used to display values
• The set() function is used to set the values of data members using parameteThe
returnSalary() function is used to return the salary of employee.
The program should create two object of the employee class, input the values and display the
record of that employee whose monthly salary is greater.

#include <iostream>
using namespace std;

class Employee {
private:
int Eid;
string Ename;
double Esalary;

public:
void get() {
cout << "Enter Employee ID, Name, and Salary: ";
cin >> Eid >> Ename >> Esalary;
}

void show() {
cout << "Employee ID: " << Eid << ", Name: " << Ename << ", Salary: " << Esalary <<
endl;
}

void set(int id, string name, double salary) {


Eid = id;
Ename = name;
Esalary = salary;
}

double returnSalary() {
return Esalary;
}
};

int main() {
Employee emp1, emp2;
emp1.get();
emp2.get();

cout << "\nEmployee with higher salary:\n";


if (emp1.returnSalary() > emp2.returnSalary()) {
emp1.show();
} else {
emp2.show();
}

return 0;
}
Question 6 Write a program that declare a class Student with five data
members to store five subject mark of student. Class also includes three
member function for input marks, Sum() to calculate and return the sum of
five subject and Avg() to calculate and return the average marks of fiv
#include <iostream>
using namespace std;

class Student {
private:
int marks[5];

public:
void getMarks() {
cout << "Enter marks for 5 subjects (out of 100): ";
for (int i = 0; i < 5; i++) {
cin >> marks[i];
}
}

int Sum() {
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += marks[i];
}
return sum;
}

double Avg() {
return Sum() / 5.0;
}
};

int main() {
Student student;
student.getMarks();

cout << "Total Marks: " << student.Sum() << endl;


cout << "Average Marks: " << student.Avg() << endl;

return 0;
}e subject. Each subject has a maximum of 100 marks. Use this class in your program.

Question 7 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.

#include <iostream>

#include <string>
using namespace std;

class Student {

private:

int admno;

string sname;

float eng, math, science;

float total;

float ctotal() { return eng + math + science;

public:

void Takedata() {

cout << "Enter Admission Number: ";

cin >> admno;

cout << "Enter Student Name: ";

cin.ignore();

getline(cin, sname);

cout << "Enter English Marks: ";

cin >> eng;

cout << "Enter Math Marks: ";

cin >> math;


cout << "Enter Science Marks: "; total = ctotal();

void Showdata() {

cout << "Admission Number: " << admno << endl;

cout << "Student Name: " << sname << endl;

cout << "English Marks: " << eng << endl;

cout << "Math Marks: " << math << endl;

cout << "Science Marks: " << science << endl;

cout << "Total Marks: " << total << endl;

};

int main() {

Student s1, s2; cout << "Enter details for Student 1:" << endl;

s1.Takedata();

cout << "\nEnter details for Student 2:" << endl;

s2.Takedata();

cout << "\nStudent 1 Details:" << endl;

s1.Showdata();
cout << "\nStudent 2 Details:" << endl;

s2.Showdata();

return 0;

QUESTION 8Define 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>

#include <string>

using namespace std;

class Batsman {

private:

int bcode;

string bname;

int innings, notout, runs;

float batavg;

void calcavg() { if (innings - notout != 0) {


batavg = (float)runs / (innings - notout);

} else {

batavg = 0.0;

public:

void readdata() {

cout << "Enter Batsman Code: ";

cin >> bcode;

cout << "Enter Batsman Name: ";

cin.ignore();

getline(cin, bname);

cout << "Enter Number of Innings: ";

cin >> innings;

cout << "Enter Number of Times Not Out: ";

cin >> notout;

cout << "Enter Total Runs: ";

cin >> runs;

calcavg();

void displaydata() {
cout << "Batsman Code: " << bcode << endl;

cout << "Batsman Name: " << bname << endl;

cout << "Number of Innings: " << innings << endl;

cout << "Number of Times Not Out: " << notout << endl;

cout << "Total Runs: " << runs << endl

; cout << "Batting Average: " << batavg << endl;

};

int main() {

Batsman b;

b.readdata();

b.displaydata();

return 0;

Question 9 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
include <iostream>

#include <string>

using namespace std;

class TEST {

private:

int TestCode;

string Description;

int NoCandidate;

int CenterReqd;

int CALCNTR() {

return (NoCandidate / 100) + 1;

public:

void SCHEDULE() {

cout << "Enter Test Code: ";

cin >> TestCode;

cout << "Enter Test Description: ";

cin.ignore();

getline(cin, Description);
cout << "Enter Number of Candidates: ";

cin >> NoCandidate;

CenterReqd = CALCNTR();

cout << "Number of Centers Required: " << CenterReqd << endl;

void DISPTEST() {

cout << "Test Code: " << TestCode << endl;

cout << "Test Description: " << Description << endl;

cout << "Number of Candidates: " << NoCandidate << endl;

cout << "Number of Centers Required: " << CenterReqd << endl;

};

int main() {

TEST t;

t.SCHEDULE();

t.DISPTEST();

return 0;

}Question 10 Define a class Flight 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

Question 11 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.

Question 12 Define a class REPORT with the following specification:


Private members :
adno 4 digit admission number
name 20 characters
marks an array of 5 floating point values
average average marks obtained
GETAVG() a function to compute the average obtained in five subject
Public members:
READINFO() function to accept values for adno, name, marks. Invoke
the function GETAVG ()
DISPLAYINFO () function to display all data members of report on the
screen.
You should give function definitions.
#include<iostream>

using namespace std ;

class REPORT

private:
int adno;

char name[20];

float marks[5], average;

void GETAVG()

int sum = 0;

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

sum = sum + marks[i];

average = sum/5.0f;

public:

void READINFO()

cout<<"Enter id ";

cin>>adno;

cout<<"Enter Name ";

cin>>name;

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

cout<<"Enter marks of subject-"<<i+1<<" ";

cin>>marks[i];

GETAVG ();#include<iostream>

using namespace std ;

class REPORT

{
private:

int adno;

char name[20];

float marks[5], average;

void GETAVG()

int sum = 0;

void DISPLAYINFO ()

cout<<"admision Id "<<adno<<endl;

cout<<"Name "<<name<<endl;

cout<<"Average "<<average<<endl;

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

cout<<"Marks of subject-"<<i+1<<" is "<<marks[i]<<endl;

int main()

REPORT R1;

R1.READINFO();

cout<<endl;

R1.DISPLAYINFO();
};

….……………………

….……………………

/// constructor with overloading

/// destructor

///

#include<iostream>

using namespace std ;

class Distance

private:

int feet;

float inches;

public:

Distance() : feet(0), inches(0.0f) {}

Distance(int f, float inc) : feet(f), inches(inc) {}

void Getdist()

cout<<"Enter feets ";

cin>>feet;

cout<<"Enter inches ";

cin>>inches;

}
void Showdist()

cout<<"Distance is "<<feet <<"\', " << inches << '\"' << endl;

///d3.Adddist(d1, d2);

Distance Adddist(Distance dd2)

Distance temp;

temp.inches = inches + dd2.inches;

while(temp.inches >= 12)

temp.inches -= 12;

temp.feet++;

temp.feet += feet + dd2.feet;

return temp;

~Distance() {}

};

int main()

Distance d1, d2(3, 10.3f), d3;

d1.Getdist(); ///1, 1.1f

d3 = d1.Adddist(d2);
cout<<"Dist1: ";

d1.Showdist();

cout<<"Dist2: ";

d2.Showdist();

cout<<"Dist3: ";

d3.Showdist();

return 0;

You might also like