Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program with a Square class containing two data members, side (type int)

ID: 3674765 • Letter: W

Question

Write a program with a Square class containing two data members, side (type int) and color (type string).

Write the necessary constructor(s) and member function to be used with the following main() function.
**************************************************************************************************************************

int main()
{
    Square s1;
    Square s2(5,"blue");
    Square s3(10,"green");

    s1.print();
    s2.print();
    s3.print();
}

**************************************************************************************************************************

Program output:

I am a red square with area 1
I am a blue square with area 25
I am a green square with area 100

Explanation / Answer

#include<iostream>
using namespace std;

class Square {
// Variable Declaration
int side;
string color;
public:

//Constructor wuithout Argument
Square() {
// Assign Values In Constructor
color="red";
side=1;
  
}
//Constructor with Argument
Square( int x,string y) {
// Assign Values In Constructor
side=x;
color=y;

}
void print() {
cout<<" I am a "<< color <<" square with area "<<side*side;
}
};

int main(){
  
Square s1;
Square s2(5,"blue");
Square s3(10,"green");
// Constructor invoked.
s1.print();
s2.print();
s3.print();
// Wait For Output Screen   
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote