0% found this document useful (0 votes)
6 views16 pages

Lab Journal 7

This lab session provides an overview of inheritance in C++. Students are asked to complete exercises on inheritance by creating classes for geometric shapes and sports players, with parent and child classes related through inheritance. Methods like constructors, get/set functions, and calculations are included as needed.

Uploaded by

saadsohail1918
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)
6 views16 pages

Lab Journal 7

This lab session provides an overview of inheritance in C++. Students are asked to complete exercises on inheritance by creating classes for geometric shapes and sports players, with parent and child classes related through inheritance. Methods like constructors, get/set functions, and calculations are included as needed.

Uploaded by

saadsohail1918
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/ 16

Lab Journal – Lab 7

Object Oriented Programming

Lab Journal - Lab 7


Name: Muhammad Saad Sohail

Enrollment #: 01-134232-203

Class: BS-CS-2(C)

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.

Public

2. Class Square is derived from class Rectangle. Write the constructor (one argument) of
class Square and call the (two argument) constructor of Rectangle.

#include <iostream>

using namespace std;

class Rectangle {
protected:
int length;
int width;

Object Oriented Programming Page 1


Lab Journal – Lab 7

public:
Rectangle(int l, int w) : length(l), width(w) {}

int area() const {


return length * width;
}
};

class Square : public Rectangle {


public:
Square(int side) : Rectangle(side, side) {}
};

int main() {
Square s(5);
cout << "Area of the square: " << s.area() << endl;
return 0;
}

3. Are overloaded operators inherited in the derived class?

yes

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;}
};

Object Oriented Programming Page 2


Lab Journal – Lab 7

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.

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

Object Oriented Programming Page 3


Lab Journal – Lab 7

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.

#include <iostream>

using namespace std;

const double PI = 3.14159;

class Point {

protected:

double x;

double y;

public:

Point(double x_val = 0, double y_val = 0) : x(x_val), y(y_val) {}

double getX() const {

return x;

Object Oriented Programming Page 4


Lab Journal – Lab 7

double getY() const {

return y;

void setX(double x_val) {

x = x_val;

void setY(double y_val) {

y = y_val;

void display() const {

cout << "(" << x << ", " << y << ")";

};

class Circle : public Point {

private:

Object Oriented Programming Page 5


Lab Journal – Lab 7

double radius;

public:

Circle(double x_val = 0, double y_val = 0, double radius_val = 0)

: Point(x_val, y_val), radius(radius_val) {}

double getRadius() const {

return radius;

void setRadius(double radius_val) {

radius = radius_val;

double computeArea() const {

return PI * radius * radius;

double computeCircumference() const {

return 2 * PI * radius;

Object Oriented Programming Page 6


Lab Journal – Lab 7

};

class Cylinder : public Circle {

private:

double height;

public:

Cylinder(double x_val = 0, double y_val = 0, double radius_val = 0, double height_val = 0)

: Circle(x_val, y_val, radius_val), height(height_val) {}

double getHeight() const {

return height;

void setHeight(double height_val) {

height = height_val;

double computeSurfaceArea() const {

Object Oriented Programming Page 7


Lab Journal – Lab 7

return 2 * computeArea() + 2 * PI * getRadius() * height;

double computeVolume() const {

return computeArea() * height;

};

int main() {

Cylinder c(0, 0, 3, 5);

cout << "Surface Area of Cylinder: " << c.computeSurfaceArea() << endl;

cout << "Volume of Cylinder: " << c.computeVolume() << endl;

return 0;

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

Object Oriented Programming Page 8


Lab Journal – Lab 7

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.

#include <iostream>

using namespace std;

const double PI = 3.14159;

class Player {

protected:

string firstName;

string lastName;

int numMatches;

string nationality;

public:

Player(string first, string last, int matches, string nation)

: firstName(first), lastName(last), numMatches(matches), nationality(nation) {}

Object Oriented Programming Page 9


Lab Journal – Lab 7

virtual void display() const {

cout << "Name: " << firstName << " " << lastName << endl;

cout << "Number of Matches: " << numMatches << endl;

cout << "Nationality: " << nationality << endl;

};

class SoccerPlayer : public Player {

private:

int goalsScored;

string position;

public:

SoccerPlayer(string first, string last, int matches, string nation, int goals, string pos)

: Player(first, last, matches, nation), goalsScored(goals), position(pos) {}

double computeAverageGoalsPerMatch() const {

if (numMatches == 0) return 0.0;

return static_cast<double>(goalsScored) / numMatches;

Object Oriented Programming Page 10


Lab Journal – Lab 7

void display() const override {

Player::display();

cout << "Goals Scored: " << goalsScored << endl;

cout << "Position: " << position << endl;

};

class CricketPlayer : public Player {

protected:

int runsScored;

int wicketsTaken;

int catchesTaken;

public:

CricketPlayer(string first, string last, int matches, string nation, int runs, int wickets, int
catches)

: Player(first, last, matches, nation), runsScored(runs), wicketsTaken(wickets),


catchesTaken(catches) {}

double computeAverageRunsPerMatch() const {

Object Oriented Programming Page 11


Lab Journal – Lab 7

if (numMatches == 0) return 0.0;

return static_cast<double>(runsScored) / numMatches;

double computeAverageWicketsPerMatch() const {

if (numMatches == 0) return 0.0;

return static_cast<double>(wicketsTaken) / numMatches;

void display() const override {

Player::display();

cout << "Runs Scored: " << runsScored << endl;

cout << "Wickets Taken: " << wicketsTaken << endl;

cout << "Catches Taken: " << catchesTaken << endl;

};

class Batsman : public CricketPlayer {

private:

int ballsFaced;

Object Oriented Programming Page 12


Lab Journal – Lab 7

int centuries;

int fifties;

public:

Batsman(string first, string last, int matches, string nation, int runs, int wickets, int catches,
int balls, int centuries_val, int fifties_val)

: CricketPlayer(first, last, matches, nation, runs, wickets, catches), ballsFaced(balls),


centuries(centuries_val), fifties(fifties_val) {}

double computeStrikeRate() const {

if (ballsFaced == 0) return 0.0;

return static_cast<double>(runsScored) / ballsFaced * 100;

void display() const override {

CricketPlayer::display();

cout << "Balls Faced: " << ballsFaced << endl;

cout << "Centuries: " << centuries << endl;

cout << "Fifties: " << fifties << endl;

};

Object Oriented Programming Page 13


Lab Journal – Lab 7

class Bowler : public CricketPlayer {

private:

int ballsBowled;

int runsConceded;

public:

Bowler(string first, string last, int matches, string nation, int runs, int wickets, int catches,
int balls, int runs_conceded)

: CricketPlayer(first, last, matches, nation, runs, wickets, catches), ballsBowled(balls),


runsConceded(runs_conceded) {}

double computeEconomyRate() const {

if (ballsBowled == 0) return 0.0;

return static_cast<double>(runsConceded) / (ballsBowled / 6);

void display() const override {

CricketPlayer::display();

cout << "Balls Bowled: " << ballsBowled << endl;

cout << "Runs Conceded: " << runsConceded << endl;

Object Oriented Programming Page 14


Lab Journal – Lab 7

};

int main() {

Batsman batsman("Babar", "Azam", 250, "Pakistan", 12000, 0, 50, 15000, 35, 25);

Bowler bowler("Jasprit", "Bumrah", 120, "India", 0, 200, 10, 8000, 2500);

cout << "Batsman Information:" << endl;

batsman.display();

cout << "Strike Rate: " << batsman.computeStrikeRate() << endl << endl;

cout << "Bowler Information:" << endl;

bowler.display();

cout << "Economy Rate: " << bowler.computeEconomyRate() << endl;

return 0;

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

S No. Exercise Checked By:


1. Exercise 1

Object Oriented Programming Page 15


Lab Journal – Lab 7

2. Exercise 2

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

Object Oriented Programming Page 16

You might also like