Lab Journal 7
Lab Journal 7
Enrollment #: 01-134232-203
Class: BS-CS-2(C)
Objective
Go through Lab 7 of the manual, attempt the given programs and verify their outputs. Once you
are done, attempt the following.
Tasks :
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>
class Rectangle {
protected:
int length;
int width;
public:
Rectangle(int l, int w) : length(l), width(w) {}
int main() {
Square s(5);
cout << "Area of the square: " << s.area() << endl;
return 0;
}
yes
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;}
};
A:parameter
B
A:default
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
Create an object of class Cylinder and compute its volume and surface area.
#include <iostream>
class Point {
protected:
double x;
double y;
public:
return x;
return y;
x = x_val;
y = y_val;
cout << "(" << x << ", " << y << ")";
};
private:
double radius;
public:
return radius;
radius = radius_val;
return 2 * PI * radius;
};
private:
double height;
public:
return height;
height = height_val;
};
int main() {
cout << "Surface Area of Cylinder: " << c.computeSurfaceArea() << 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.
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>
class Player {
protected:
string firstName;
string lastName;
int numMatches;
string nationality;
public:
cout << "Name: " << firstName << " " << lastName << endl;
};
private:
int goalsScored;
string position;
public:
SoccerPlayer(string first, string last, int matches, string nation, int goals, string pos)
Player::display();
};
protected:
int runsScored;
int wicketsTaken;
int catchesTaken;
public:
CricketPlayer(string first, string last, int matches, string nation, int runs, int wickets, int
catches)
Player::display();
};
private:
int ballsFaced;
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::display();
};
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::display();
};
int main() {
Batsman batsman("Babar", "Azam", 250, "Pakistan", 12000, 0, 50, 15000, 35, 25);
batsman.display();
cout << "Strike Rate: " << batsman.computeStrikeRate() << endl << endl;
bowler.display();
return 0;
Implement the given exercises and get them checked by your instructor.
2. Exercise 2
+++++++++++++++++++++++++