blog
blog copied to clipboard
C++ Separating Class Specification from Implementation
Example:
#include <iostream>
using namespace std;
class Rectangle {
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 functions
double getLength() const {
// Add the keyword const to the accessor function, can not change the variable
// length = 1;
return length;
}
double getWidth() const {
return width;
}
void setLength(double new_length) {
length = new_length;
}
void setWidth(double new_width) {
width = new_width;
}
double getPerimeter() const {
double perimeter = (length + width) * 2;
return perimeter;
}
double getArea() const {
double area = length * width;
return area;
}
};
int main() {
// 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>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
// Function prototypes:
// Rectangle();
Rectangle(double, double);
double getLength() const;
double getWidth() const;
void setLength(double new_length);
void setWidth(double new_width);
double getPerimeter() const;
double getArea() 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;
}
double Rectangle::getLength() const {
// Add the keyword const to the accessor function, can not change the variable
// length = 1;
return length;
}
double Rectangle::getWidth() const {
return width;
}
void Rectangle::setLength(double new_length) {
length = new_length;
}
void Rectangle::setWidth(double new_width) {
width = new_width;
}
double Rectangle::getPerimeter() const {
double perimeter = (length + width) * 2;
return perimeter;
}
double Rectangle::getArea() const {
double area = length * width;
return area;
}
int main() {
// 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 constructor
return 0;
}
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. |
rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle {
private:
double length;
double width;
public:
// Function prototypes:
// Constructor
// Rectangle();
Rectangle(double, double);
double getLength() const;
double getWidth() const;
void setLength(double new_length);
void setWidth(double new_width);
double getPerimeter() const;
double getArea() const;
};
#endif
rectangle.cpp
#include "rectangle.h"
// Overload Constructor:
//Rectangle::Rectangle() {
// length = 1;
// width = 1;
//}
Rectangle::Rectangle(double new_len, double new_wid) {
length = new_len;
width = new_wid;
}
double Rectangle::getLength() const {
// Add the keyword const to the accessor function, can not change the variable
// length = 1;
return length;
}
double Rectangle::getWidth() const {
return width;
}
void Rectangle::setLength(double new_length) {
length = new_length;
}
void Rectangle::setWidth(double new_width) {
width = new_width;
}
double Rectangle::getPerimeter() const {
double perimeter = (length + width) * 2;
return perimeter;
}
double Rectangle::getArea() const {
double area = length * width;
return area;
}
main.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main() {
// 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;
return 0;
}