You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<iostream>usingnamespacestd;classRectangle {
private:double length;
double width;
public:// Constructor will be created automatically, but nothing inside// Overload// Rectangle() {// length = 1;// width = 1;// }// When all of a class's constructors require arguments, then the class does not have a default constructor.// In such case, you must pass the required arguments to the constructor when creating an object.// Otherwise, a compiler error will result.// So, in main function, we must put the arguments: eg: Rectangle myRectangle(1, 1);Rectangle(double new_len, double new_wid) {
length = new_len;
width = new_wid;
}
// "getter functions" aka accessor functions// "setter functions" aka mutator functionsdoublegetLength() const {
// Add the keyword const to the accessor function, can not change the variable// length = 1;return length;
}
doublegetWidth() const {
return width;
}
voidsetLength(double new_length) {
length = new_length;
}
voidsetWidth(double new_width) {
width = new_width;
}
doublegetPerimeter() const {
double perimeter = (length + width) * 2;
return perimeter;
}
doublegetArea() const {
double area = length * width;
return area;
}
};
intmain() {
// Rectangle myRectangle, smallRectangle, largeRectangle;
Rectangle myRectangle(1, 1), smallRectangle(0.5, 0.5), largeRectangle(1.5, 1.5);
smallRectangle.setLength(1.5);
smallRectangle.setWidth(1.7);
cout << "smallRectangle's length is: " << smallRectangle.getLength() << endl;
cout << "smallRectangle's width is: " << smallRectangle.getWidth() << endl;
largeRectangle.setLength(100.7);
largeRectangle.setWidth(150.6);
cout << "The perimeter of the large rectangle is: ";
cout << largeRectangle.getPerimeter() << endl;
cout << "The area of the large rectangle is: ";
cout << largeRectangle.getArea() << endl;
cout << "myRectangle's length is: " << myRectangle.getLength() << endl;
}
Or:
#include<iostream>usingnamespacestd;classRectangle {
private:double length;
double width;
public:// Function prototypes:// Rectangle();Rectangle(double, double);
doublegetLength() const;
doublegetWidth() const;
voidsetLength(double new_length);
voidsetWidth(double new_width);
doublegetPerimeter() const;
doublegetArea() const;
};
// class name + double colons (the scope resolution operator),// means that functions belong to the specified class.//Rectangle::Rectangle() {// length = 1;// width = 1;//}Rectangle::Rectangle(double new_len, double new_wid) {
length = new_len;
width = new_wid;
}
doubleRectangle::getLength() const {
// Add the keyword const to the accessor function, can not change the variable// length = 1;return length;
}
doubleRectangle::getWidth() const {
return width;
}
voidRectangle::setLength(double new_length) {
length = new_length;
}
voidRectangle::setWidth(double new_width) {
width = new_width;
}
doubleRectangle::getPerimeter() const {
double perimeter = (length + width) * 2;
return perimeter;
}
doubleRectangle::getArea() const {
double area = length * width;
return area;
}
intmain() {
// Rectangle myRectangle, smallRectangle, largeRectangle;
Rectangle myRectangle(1, 1), smallRectangle(0.5, 0.5), largeRectangle(1.5, 1.5);
smallRectangle.setLength(1.5);
smallRectangle.setWidth(1.7);
cout << "smallRectangle's length is: " << smallRectangle.getLength() << endl;
cout << "smallRectangle's width is: " << smallRectangle.getWidth() << endl;
largeRectangle.setLength(100.7);
largeRectangle.setWidth(150.6);
cout << "The perimeter of the large rectangle is: ";
cout << largeRectangle.getPerimeter() << endl;
cout << "The area of the large rectangle is: ";
cout << largeRectangle.getArea() << endl;
cout << "myRectangle's length is: " << myRectangle.getLength() << endl; // 1. set in constructorreturn0;
}
Output:
smallRectangle's length is: 1.5
smallRectangle's width is: 1.7
The perimeter of the large rectangle is: 502.6
The area of the large rectangle is: 15165.4
myRectangle's length is: 1
Separating Class Specification, Implementation, and Client Code
Concept: Usually class declarations are stored in their own header files. Member function definitions are stored in their own .cpp files.
Class Specification
Class Implementation
Client Code
ClassName.h (header file)
ClassName.cpp
xxx.cpp
Contains the class declaration (with member function prototypes and member variables).
Contains member function definitions for the class. Remember to use the scope resolution operator :: .
The part of the program that uses the class - creates and uses the objects.
Example:
Or:
Output:
Separating Class Specification, Implementation, and Client Code
ClassName.h
(header file)ClassName.cpp
xxx.cpp
::
.rectangle.h
rectangle.cpp
main.cpp
The text was updated successfully, but these errors were encountered: