OOP Assignment

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Lab Journal – Lab 7

Object Oriented Programming

Lab Journal - Lab 7


Name: Muhammad Saad Sohail

Enrollment #: 01-134232-203

Class: BS-CS-(2C)

Objective

This lab session is intended to provide an overview of Inheritance in C++.

Go through Lab 7 of the manual, attempt the given programs and verify their outputs. Once you
are done, attempt the following.

Tasks :

Answer the following/Write C++ code where required.

1. class A: public class B { }

State which members (public/protected/private) of class A can be access from class B.


In the given code, class B has access to all public members of class A. If class A has any public
methods, variables, or nested classes, class B can access them directly. However, class B
cannot access any private or protected members of class A, as they are only accessible within
class A and its inner classes.
2. Class Square is derived from class Rectangle. Write the constructor (one argument) of
class Square and call the (two argument) constructor of Rectangle.
public class Rectangle {
private double width;
private double height;

public Rectangle(double width, double height) {


this.width = width;
this.height = height;
}

// Getters and setters for width and height


}

public class Square extends Rectangle {


public Square(double sideLength) {
super(sideLength, sideLength);
}

Object Oriented Programming Page 1


Lab Journal – Lab 7

// Additional methods for Square if needed


}
3. Are overloaded operators inherited in the derived class?
In C++, overloaded operators are not automatically inherited by derived classes. However, they
can be inherited if the operator is defined as a member function or a non-static member function
of the base class, and the derived class does not provide its own definition for the operator.
4. Consider the following code:

class A {
public:
A ( )
{cout<<“A:default”<<endl;}

A (int a)
{cout<<“A:parameter”<<endl;}
};

class B : public A
{
public:
B (int a)
{cout<<“B”<<endl;}
};

Write the output of:


B b(2);
B b2;
A:parameter
B
A:default
5. The class B in the above question is changed as follows:
class B : public A
{
public:
B (int a): A(a)
{cout<<“B”<<endl;}
};

What is the output of:


B test(5);
A:parameter
B
Exercise 1

Create a class Point with two data members x and y. Provide appropriate
constructors, get, set and display methods.

Object Oriented Programming Page 2


Lab Journal – Lab 7

Derive a class Circle from Point. The circle, in addition to the center (Point) also
has radius as its data member. Provide constructors, get and set methods in the
circle class. Also provide methods to compute area and circumference of the
circle. (Area of circle is PI * r * r and Circumference is: 2 * PI * r).

Derive a class Cylinder from circle with a data member height. Provide
constructors and set/get methods to set/get height of the cylinder. Provide a
function to compute area of cylinder. (A = 2 x area of circle + 2*PI*r*height). Also
provide a function to compute the volume of cylinder (PI * r*r*h).

Create an object of class Cylinder and compute its volume and surface area.

 Input :

#include <iostream>
#include <cmath>

const double PI = 3.14159265358979323846;

class Point {
public:
Point(double x = 0.0, double y = 0.0) : x_(x), y_(y) {}

double getX() const { return x_; }


double getY() const { return y_; }

void setX(double x) { x_ = x; }
void setY(double y) { y_ = y; }

void display() const {


std::cout << "(" << x_ << ", " << y_ << ")";
}

private:
double x_, y_;
};

class Circle : public Point {


public:
Circle(double x = 0.0, double y = 0.0, double r = 0.0) : Point(x, y), radius_(r) {}

double getRadius() const { return radius_; }


void setRadius(double r) { radius_ = r; }

double area() const { return PI * radius_* radius_; }


double circumference() const { return 2 * PI* radius_; }

protected:
double radius_;
};

Object Oriented Programming Page 3


Lab Journal – Lab 7

class Cylinder : public Circle {


public:
Cylinder(double x = 0.0, double y = 0.0, double r = 0.0, double h = 0.0)
: Circle(x, y, r), height_(h) {}

double getHeight() const { return height_; }


void setHeight(double h) { height_ = h; }

double surfaceArea() const {


return 2 * area() + 2 * PI * radius_ * height_;
}

double volume() const { return PI * radius_* radius_* height_; }

private:
double height_;
};

int main() {
Cylinder cylinder(0.0, 0.0, 1.0, 2.0);

std::cout << "Center: ";


cylinder.display();
std::cout << "\nRadius: " << cylinder.getRadius() << "\nHeight: " <<
cylinder.getHeight()
<< "\nArea: " << cylinder.surfaceArea() << "\nVolume: " << cylinder.volume()
<< std::endl;

return 0;
}

 Output :

Exercise 2

Create a class Player with data members: first name, last name, number of
matches and nationality. Derive two classes SoccerPlayer and CricketPlayer from

Object Oriented Programming Page 4


Lab Journal – Lab 7

Player. The class SoccerPlayer should have the following members: a variable to
store the number of goals the player has scored and a variable to store the
position on which the player plays. The class should also have a member function
to compute the average number of goals scored per match.
The class CricketPlayer should have variables to store the number of runs, the
number of wickets and the number of catches a player has taken. Provide member
functions to compute the average runs scored and average wickets taken per
match by a player.
Derive two classes Batsman and Bowler from the class CricketPlayer. For bats
men, also store the total number of balls faced, number of 100s and number of
50s. For bowlers, store the number of balls bowled and runs conceded. For bats
men provide a function to compute runs scored per 100 balls (strike rate) and for
bowlers provide methods to calculate wickets taken and runs conceded per
hundred balls.

 Input:

#include <iostream>
#include <string>
using namespace std;

class Player {
protected:
string firstName;
string lastName;
int numberOfMatches;
string nationality;

public:
Player(string fn, string ln, int nm, string nat)
: firstName(fn), lastName(ln), numberOfMatches(nm), nationality(nat) {}

virtual ~Player() {}
};

class SoccerPlayer : public Player {


private:
int numberOfGoals;
string position;

public:
SoccerPlayer(string fn, string ln, int nm, string nat, int goals, string pos)
: Player(fn, ln, nm, nat), numberOfGoals(goals), position(pos) {}

double averageGoalsPerMatch() {
return numberOfMatches > 0 ? static_cast<double>(numberOfGoals) /
numberOfMatches : 0.0;
}
};

class CricketPlayer : public Player {

Object Oriented Programming Page 5


Lab Journal – Lab 7

protected:
int numberOfRuns;
int numberOfWickets;
int numberOfCatches;

public:
CricketPlayer(string fn, string ln, int nm, string nat, int runs, int wickets, int
catches)
: Player(fn, ln, nm, nat), numberOfRuns(runs), numberOfWickets(wickets),
numberOfCatches(catches) {}

double averageRunsPerMatch() {
return numberOfMatches > 0 ? static_cast<double>(numberOfRuns) /
numberOfMatches : 0.0;
}

double averageWicketsPerMatch() {
return numberOfMatches > 0 ? static_cast<double>(numberOfWickets) /
numberOfMatches : 0.0;
}
};

class Batsman : public CricketPlayer {


private:
int totalBallsFaced;
int numberOfHundreds;
int numberOfFifties;

public:
Batsman(string fn, string ln, int nm, string nat, int runs, int wickets, int
catches, int ballsFaced, int hundreds, int fifties)
: CricketPlayer(fn, ln, nm, nat, runs, wickets, catches),
totalBallsFaced(ballsFaced), numberOfHundreds(hundreds), numberOfFifties(fifties) {}

double strikeRate() {
return totalBallsFaced > 0 ? (static_cast<double>(numberOfRuns) /
totalBallsFaced) * 100 : 0.0;
}
};

class Bowler : public CricketPlayer {


private:
int totalBallsBowled;
int runsConceded;

public:
Bowler(string fn, string ln, int nm, string nat, int runs, int wickets, int
catches, int ballsBowled, int conceded)
: CricketPlayer(fn, ln, nm, nat, runs, wickets, catches),
totalBallsBowled(ballsBowled), runsConceded(conceded) {}

double wicketsPerHundredBalls() {
return totalBallsBowled > 0 ? (static_cast<double>(numberOfWickets) /
totalBallsBowled) * 100 : 0.0;
}

double runsConcededPerHundredBalls() {

Object Oriented Programming Page 6


Lab Journal – Lab 7

return totalBallsBowled > 0 ? (static_cast<double>(runsConceded) /


totalBallsBowled) * 100 : 0.0;
}
};

// Example usage
int main() {
SoccerPlayer sp("Lionel", "Messi", 800, "Argentina", 700, "Forward");
cout << "Average Goals Per Match: " << sp.averageGoalsPerMatch() << endl;

Batsman batsman("Sachin", "Tendulkar", 463, "India", 18426, 0, 140, 21367, 49, 96);
cout << "Batsman's Strike Rate: " << batsman.strikeRate() << endl;

Bowler bowler("Shane", "Warne", 145, "Australia", 3000, 708, 125, 36210, 17695);
cout << "Wickets Per Hundred Balls: " << bowler.wicketsPerHundredBalls() << endl;
cout << "Runs Conceded Per Hundred Balls: " << bowler.runsConcededPerHundredBalls()
<< endl;

return 0;
}

 Output :

Implement the given exercises and get them checked by your instructor.

S No. Exercise Checked By:


1. Exercise 1

2. Exercise 2

+++++++++++++++++++++++++

Object Oriented Programming Page 7


Lab Journal – Lab 7

Object Oriented Programming Page 8

You might also like