Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ Separating Class Specification from Implementation #212

Open
Qingquan-Li opened this issue Jul 6, 2022 · 0 comments
Open

C++ Separating Class Specification from Implementation #212

Qingquan-Li opened this issue Jul 6, 2022 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Jul 6, 2022

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;
}
@Qingquan-Li Qingquan-Li added the C++ label Jul 6, 2022
@Qingquan-Li Qingquan-Li changed the title C++ Classes C++ Separating Class Specification from Implementation Jul 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant