I am trying to understand several programs in my class can s
I am trying to understand several programs in my class.
can someone explain the lines of code
Rectangle(float h, float w):height(h),width(w)
AND
Square(float side): Rectangle(side,side) {} ?
Thanks!
class Rectangle {
public:
Rectangle(float h, float w):height(h),width(w){
}
float height, width;
};
class Square : public Rectangle{
public:
Square(float side): Rectangle(side,side) {}
};
Solution
#include<iostream>
using namespace std;
//Rectangle class
class Rectangle
{
//private data members
float height, width;
public:
//Constructor that takes h and w as sarguments
//set values of h and w to private variables of
//this class height and width
Rectangle(float h, float w):height(h),width(w)
{
}
};
//Square class that inherits from Rectangle
class Square : public Rectangle
{
public:
//The constructor Square that takes side as input arguments
//and pass the argument to its parent class Rectangle both arguments with
//side value
Square(float side): Rectangle(side,side)
{}
};
//Sample main method to demonstrate the use of Rectangel and Square class
void main()
{
//create an object of Rectangle class with height=1 and width=2
Rectangle r(1,2);
//create an object of Square class with side=1
//The object inturn calls the rectangle class to set width and height with side value=1
Square s(1);
system(\"pause\");
}

