Sie sind auf Seite 1von 13

Assignment 1 – Object oriented Programming Lab

BSCYS-f-19-A, Computer Science Department Air University

Student Name : Abdullah Bashir


Roll Number : 180437
Class : Cyber Security
Section : 1- A
Date : 4th February,2020
Subject : Object oriented Programming
Submitted to : Madam Fatima Noor Ul Ain
Question: 1

Write a class definition Distance with two data members feet and inches
This class also contains member functions

• void setFeet(int)
• void setInches( float)
• void showDistance()
Demonstrate the class by writing a complete program implementing it.
Source code :

#include <iostream>

using namespace std;

class distancce

{private:

double feet;

double inches;

public:

void setfeet(double f)

feet=f;

void setinches(double i)

inches=i;

double getfeet()

return feet;

double getinches()

{
return inches;

};

int main()

{ distancce d;

double feet,inches;

d.setfeet(2.6);

d.setinches(3.7);

cout<<"Set inches and feet are "<<d.getfeet()<<"\t"<<d.getinches()<<endl;

cout<<"Enter feet and inches"<<endl;

cin>>feet;

cin>>inches;

d.setfeet(feet);

d.setinches(inches);

cout<<"Entered inches and feet are "<<d.getfeet()<<"\t"<<d.getinches()<<endl;

return 0;

Output:
Question: 2

Write a class declaration named Circle with a private member variable named radius. Write set and

get functions to access the radius variable, and a function named getArea that returns the area of the

circle. The area is calculated as

Area = 3.14159 * radius * radius

Source code :

#include <iostream>

using namespace std;

class area

{private:

double radius;

public:

void setradius(double r)

radius=r;

double getradius()

return radius;

double getarea()

return radius*radius*3.14;

};

int main()

{ area a;

double radius;
a.setradius(2.6);

cout<<"Set Radius is "<<a.getradius()<<endl;

cout<<"Area for the set Radius is "<<a.getarea()<<endl;

cout<<"Enter Radius"<<endl;

cin>>radius;

a.setradius(radius);

cout<<"Set Radius is "<<a.getradius()<<endl;

cout<<"Area for the set Radius is "<<a.getarea()<<endl;

return 0;

Output:
Question: 3

Define a class Student with the following specification

Private members of class

• Registration Number
• Name
• English, Math, Science
• Total

Public member function of class:

• Setter & Getter Methods for registration number, name, eng, math, science
• CalculateTotal( )a function to calculate english + math + science with float return type.
• Showdata( ) Function to display all the data members on the screen.

Source code :

#include <iostream>

using namespace std;

class student

{private:

string name;

int idnumber;

float math;

float science;

float English;

float total;

public:

void setname(string n)

name=n;

void setidnumber(int x)

{
idnumber=x;

void setmath(int y)

math=y;

void setscience(int z)

science=z;

void setEnglish(int q)

English=q;

string getname()

return name;

int getid()

return idnumber;

double getscience()

return science;

double getmath()

return math;
}

double getEnglish()

return English;

float gettotal()

return English+math+science;

void showdata()

cout<<"Name="<<getname()<<endl;

cout<<"Id is "<<getid()<<endl;

cout<<"Marks of English = "<<getEnglish()<<endl;

cout<<"Marks of Maths ="<<getmath()<<endl;

cout<<"Marks of Science ="<<getscience()<<endl;

cout<<"Total ="<<gettotal()<<endl;

};

int main()

{ student s;

int q;

double x,y,z;

string p;

cout<<"Enter the name of the student"<<endl;

cin>>p;

cout<<"Enter the id of the student"<<endl;

cin>>q;

cout<<"Enter the marks of English math science "<<endl;


cin>>x>>y>>z;

s.setname(p);

s.setidnumber(q);

s.setEnglish(x);

s.setmath(y);

s.setscience(z);

s.showdata();

Output:
Question: 4

Define a class Batsman with the following specifications:

Private members:

• Bcode int
• Bname string
• Inningsint
• Notout int
• Runs int
• Batavg it is calculated according to the formula batavg =runs/(innings-notout)

Public members:

• Setter Methods for Bcode, Bname, innings, notout and runs


• calcavg( ) Function to compute Batavg
• Getter Methods to display the data members on the screen.

Source code :

#include <iostream>

using namespace std;

class batman

{private:

string bname;

int bcode;

int innings;

int notout;

int run;

float batavg;

public:

void setbname(string n)

bname=n;

void setbcode(int x)
{

bcode=x;

void setinnings(int y)

innings=y;

void setnotout(int z)

notout=z;

void setrun(int q)

run=q;

string getbname()

return bname;

int getbcode()

return bcode;

int getinnings()

return innings;

int getnotout()

{
return notout;

int getrun()

return run;

float getbatavg()

return (run)/float(innings-notout);

void showdata()

cout<<"Name="<<getbname()<<endl;

cout<<"Id is "<<getbcode()<<endl;

cout<<"innings is = "<<getinnings()<<endl;

cout<<"Not pout is ="<<getnotout()<<endl;

cout<<"Run is ="<<getrun()<<endl;

cout<<"Bat average is ="<<getbatavg()<<endl;

};

int main()

{ batman s;

int q;

int x,y,z;

string p;

cout<<"Enter the name of the batsman"<<endl;

cin>>p;

cout<<"Enter the code of the batsman"<<endl;

cin>>q;
cout<<"Enter the no of innnings"<<endl;

cin>>x;

cout<<"Enter the no of notout"<<endl;

cin>>y;

cout<<"Enter the run"<<endl;

cin>>z;

s.setbname(p);

s.setbcode(q);

s.setinnings(x);

s.setnotout(y);

s.setrun(z);

s.showdata();

Output:

Das könnte Ihnen auch gefallen