Below is some code for a rectangle class that needs to be completed. Add member function declarations to the class declaration and member function definitions below the declaration. For the accessor functions, you can add the definitions directly into the class declaration. The goal is to code the class so that it works wthout changing the main program #include using namespace std; // rectangle has a vertical height and horizontal width // The class below is a rectangle. It has two private // data members: height and width. // TODO: Complete the class declaration and definition. class rectangle { public: // TODO: declare a default constructor // Make the height and width = 1. // TODO: declare member function void add // @param int height, int width // TODO: delcare member function void set // @param int height, int width // TODO: declare member function void draw // TODO: define accessor for width // TODO: define accessor for height private: int width; int height; }; // TODO: define the default constructor that // sets height and width to 1. rectangle::rectangle() { } // TODO: Implement add to increment the length void rectangle::add(int h, int w) { } // TODO: Implement set to overwrite the data members void rectangle::set(int h, int w) { } // TODO: Implement draw to draw a rectangle using '*' characters void rectangle::draw() { } // TODO: Don't forget to define getWidth and getHeight int main() { // Declare 2 rectangles rectangle r1, r2; // Print the unit rectangle cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl; // Set, print dimensions and draw r1.set(4, 3); cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl; r1.draw(); // Assign, increment, print dimensions and draw r2 = r1; r2.add(3, 4); cout << "r2 is " << r2.getHeight() << " x " << r2.getWidth() << endl; r2.draw(); return 0; }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
Below is some code for a rectangle class that needs to be completed. Add member function declarations to the class declaration and member function definitions below the declaration. For the accessor functions, you can add the definitions directly into the class declaration. The goal is to code the class so that it works wthout changing the main program
#include <iostream>
using namespace std;
// rectangle has a vertical height and horizontal width
// The class below is a rectangle. It has two private
// data members: height and width.
// TODO: Complete the class declaration and definition.
class rectangle {
public:
// TODO: declare a default constructor
// Make the height and width = 1.
// TODO: declare member function void add
// @param int height, int width
// TODO: delcare member function void set
// @param int height, int width
// TODO: declare member function void draw
// TODO: define accessor for width
// TODO: define accessor for height
private:
int width;
int height;
};
// TODO: define the default constructor that
// sets height and width to 1.
rectangle::rectangle()
{
}
// TODO: Implement add to increment the length
void rectangle::add(int h, int w)
{
}
// TODO: Implement set to overwrite the data members
void rectangle::set(int h, int w)
{
}
// TODO: Implement draw to draw a rectangle using '*' characters
void rectangle::draw()
{
}
// TODO: Don't forget to define getWidth and getHeight
int main()
{
// Declare 2 rectangles
rectangle r1, r2;
// Print the unit rectangle
cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl;
// Set, print dimensions and draw
r1.set(4, 3);
cout << "r1 is " << r1.getHeight() << " x " << r1.getWidth() << endl;
r1.draw();
// Assign, increment, print dimensions and draw
r2 = r1;
r2.add(3, 4);
cout << "r2 is " << r2.getHeight() << " x " << r2.getWidth() << endl;
r2.draw();
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images