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

Opp All Program File

The document contains multiple programming tasks involving the creation of classes in C++. It includes examples of classes for handling data such as integers, distances, time, and student records, with functions for inputting and displaying data. Each task demonstrates object-oriented programming principles, including encapsulation and member functions.

Uploaded by

atshamramzan6018
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)
2 views

Opp All Program File

The document contains multiple programming tasks involving the creation of classes in C++. It includes examples of classes for handling data such as integers, distances, time, and student records, with functions for inputting and displaying data. Each task demonstrates object-oriented programming principles, including encapsulation and member functions.

Uploaded by

atshamramzan6018
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/ 10

Name: Muhammad Ehtasham

Roll No: 007

Question no 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 Myclass

private:

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 no 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 << "Distance: " << feet << " feet " << inches << " inches" << endl;

};

int main() {

Distance d1, d2

d1.getDist(5, 8.5);

d2.getDist(3, 6.2);

d1.showDist();

d2.showDist();

return 0;

Question no 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 << "Time: " << hours << ":" << minutes << ":" << seconds << endl;

};

int main() {

TIME t1, t2;

t1.setTime(10, 45, 30);

t2.setTime(5, 15, 20);

t1.showTime();

t2.showTime();

return 0;

Question no 07

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>

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;

}
Question no 04

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.

#include <iostream>

using namespace std;

class Person {
private:

int Pid;

char Pname[50];

float PSalary;

public:

void getdata() {

cout << "Enter Person ID: ";

cin >> Pid;

cin.ignore();

cout << "Enter Person Name: ";

cin.getline(Pname, 50);

cout << "Enter Person Salary: ";

cin >> PSalary;

void showdata() {

cout << "\nPerson Details:\n";

cout << "ID: " << Pid << "\n";

cout << "Name: " << Pname << "\n";

cout << "Salary: " << PSalary << "\n";

}
void setdata(int id, const char name[], float salary) {

Pid = id;

strcpy(Pname, name);

PSalary = salary;

float getSalary() {

return PSalary;

};

int main() {

Person p1, p2, p3;

cout << "Enter details for Person 1:\n";

p1.getdata();

cout << "Enter details for Person 2:\n";

p2.getdata()

cout << "Enter details for Person 3:\n";

p3.getdata();

// Find the highest salary holder

Person highest = p1;

if (p2.getSalary() > highest.getSalary()) {

highest = p2;

if (p3.getSalary() > highest.getSalary()) {


highest = p3;

// Display the highest salary holder's details

cout << "\nPerson with Highest Salary:";

highest.showdata();

return 0;

You might also like