InheritanceLab CPP
InheritanceLab CPP
InheritanceLab CPP
class Line {
public:
Line(int l) {
length = l;
}
int GetLength() {
return length;
}
void DrawLine() {
for (int i = 0; i < length; i++) {
cout << '*';
}
cout << endl;
}
private:
int length;
};
To test our Line class, create its object and then call the DrawLine function
on it in main.
//add code below this line
Line line(10);
line.DrawLine();
Box has one attribute, int width, which will present the width of the Box
object. The Box constructor takes two parameters, one of which is presented
by width and the other is presented by length which is inherited from the
Line constructor. Box has two class functions, the getter GetWidth and
DrawBox. Notice how inheritance enables us to borrow functions and
attributes from the base class to further extend the derived class.
DrawBox utilizes the width attribute to tell the system how many times to
call DrawLine. The end result is a draw of a “box” that is created from
multiple “lines”.
//add class definitions below this line
class Line {
public:
Line(int l) {
length = l;
}
int GetLength() {
return length;
}
void DrawLine() {
for (int i = 0; i < length; i++) {
cout << '*';
}
cout << endl;
}
private:
int length;
};
int GetWidth() {
return width;
}
void DrawBox() {
for (int i = 0; i < width; i++) {
DrawLine();
}
}
private:
int width;
};
challenge
Given Code
#include <iostream>
using namespace std;
class Line {
public:
Line(int l) {
length = l;
}
int GetLength() {
return length;
}
void DrawLine() {
for (int i = 0; i < length; i++) {
cout << '*';
}
cout << endl;
}
private:
int length;
};
void DrawBox() {
for (int i = 0; i < width; i++) {
DrawLine();
}
}
private:
int width;
};
int main() {
return 0;
Our new derived class is called Pattern. This class builds off of Box and Line
by utilizing their getter functions GetLength and GetWidth. First we need to
build the Pattern constructor which inherits the Box constructor exactly.
Then we will create a new function called DrawPattern that will output a
modified “box” with a pattern. Note that Pattern does not have any private
members. It is simply an extension of the Box class.
class Line {
public:
Line(int l) {
length = l;
}
int GetLength() {
return length;
}
void DrawLine() {
for (int i = 0; i < length; i++) {
cout << '*';
}
cout << endl;
}
private:
int length;
};
int GetWidth() {
return width;
}
void DrawBox() {
for (int i = 0; i < width; i++) {
DrawLine();
}
}
private:
int width;
};
void DrawPattern() {
for (int i = 0; i < GetLength(); i++) {
if (i % 2 == 0) {
for (int j = 0; j < GetWidth(); j++) {
if ( (j % 2 == 0) ) {
cout << '*';
}
else {
cout << ' ';
}
}
cout << endl;
}
if (i % 2 == 1) {
for (int j = 0; j < GetWidth(); j++) {
if ( (j % 2 == 0) ) {
cout << ' ';
}
else {
cout << '*';
}
}
cout << endl;
}
}
}
};
Problem
In the IDE to the left, the class MP3 is already defined. Complete the class
Podcast that inherits from MP3. This class should do the following things:
* Inherit the constructor such that Podcast has the following attributes:
* title - a string that is the title of the episode
* length - an integer that has the length of the podcast in seconds
* genre - a string that is the genre of the podcast
* name - a string that is the name of the podcast
* date - a string that represents when the podcast was released to the public
* DO NOT EDIT the specified code or you might not receive credit for your
work!
Note that a few of the attributes are present in both the MP3 and Podcast
classes. To connect their constructors, you can use the same parameter
values for the attributes that are the same, then use different parameter
values for the attributes that are different. Finally, set the attributes to the
parameters appropriately for the ones that are different. For example:
Given Code
#include <iostream>
using namespace std;
class MP3 {
public:
MP3(string t, int l, string g, string al, string ar) {
title = t;
album = al;
length = l;
genre = g;
artist = ar;
}
string GetTitle() {
return title;
}
int GetLength() {
return length;
}
string GetGenre() {
return genre;
}
string GetAlbum() {
return album;
}
string GetArtist() {
return artist;
}
private:
string title;
int length;
string genre;
string album;
string artist;
};
int main() {
return 0;
Expected Output