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

tps://dccod. blackboard.com/webapps/blackboard/content/listContentjsp?course ids

ID: 3736289 • Letter: T

Question

tps://dccod. blackboard.com/webapps/blackboard/content/listContentjsp?course ids 223020 1cor Lab 9 te 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 sze 8 and draw it Optional Chalenge 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 vaniable names 5 Overall neat organization 6 Comments in code explain what's being done 7 Correct division of code into h and cpp fles 8 Use of #pragma once in h ties (or #mden 9 #rciude "si darm" in cpp files (or suppress pch)

Explanation / Answer

/*.h file*/

#ifndef SQUARE_H
#define SQUARE_H

class square{
private:
    int size;
public:
    /*constructor overloading*/
    square(int size);
    void draw();
};
/*optional challenge*/
class triangle{
private:
    int size;
public:
    /*constructor overloading*/
    triangle(int size);
    void draw();
};

#endif /* SQUARE_H */

/*.cpp file*/

#include "shape.h"
#include <iostream>
using namespace std;
square::square(int size){
    this->size = size;
}
void square::draw(){
    for(int i= 0; i < size; i ++){/*Total number of rows to be printed is equal to size of the square*/
        for(int j = 0; j < size; ++j){/*Total number of * to be printed is equal to the size of square */
            cout<<"*";
        }
        cout<<endl;/*need to end the line after every row*/
    }
}
/*optional challenge*/
triangle::triangle(int size){
    this->size = size;
}

void triangle::draw(){
    for(int i= 0; i < size; i ++){/*Total number of rows to be printed is equal to size of the triangle*/
        for(int j = 0; j <= i; ++j){/*total number of * to be printed in given row is equal to the row number itself(if considered that row number starts from 1)*/
            cout<<"*";
        }
        cout<<endl;
    }
}

/*main file */

#include <cstdlib>
#include "shape.h"

using namespace std;

int main(int argc, char** argv) {
    square sq5(5),sq8(8);
    sq5.draw();
    sq8.draw();
    /*optional challenge*/
    triangle trng(4);
    trng.draw();

    return 0;
}

/*OUTPUT*/

/*

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


*/