e http://dcod.blackboard.com/webapps blackboard/ontent itContentjsp?course id 22
ID: 3735961 • Letter: E
Question
e http://dcod.blackboard.com/webapps blackboard/ontent itContentjsp?course id 2230201 (110 e Lab 9 1. Create a class called Square using separate h and cpp files. Squares look something like this when drawn: 2. Add a constructor that takes the size as a parameter. Store the size in a class variable 3. Add a Draw method that draws the square. The draw method does not take any parameters. 4. Create a main) that instantiates a square of size 5 and draws it 5. Also instantiate a square of size 8 and draw it Optional Challenge: Create a class called Triangle that draws triangles of a specified height. Here is a triangle of height 4: Checklist 1. Project/solution named correctly 2. Correct comments at top 3 Consistent indentation (Use Edit/Advanced/Format Document) 4 Good variable namesERE 5 Overall neat organization 6. Comments in code explain what's being done 7 Correct division of code into h and cpp riles 8 Use of #pragma once in h files (or #mden 9. #include stdann, in cpp files (or suppress pch)Explanation / Answer
The c++ code for the above question is as follows:-
#include <bits/stdc++.h>
using namespace std;
class Square //declaring a class Square
{
int size,i,j;
public:
Square(int s){ //Constructor with one parameter
size=s; //initialsing class size variable
}
public:
void draw() //draw function with no parameters
{
cout<<"Square of size "<<size<<" is "<<" ";
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
cout<<"*";
}
cout<<" ";
}
}
};
int main()
{
Square sq(5); //Square Object initialsing constructor with size 5
sq.draw(); //calling the draw method using Square object
Square sq1(8);
sq1.draw();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.