How to write code for this? - Class

Hey all, I’m very new to c++ and trying to learn from doing exercises. I kind of stuck and need help.


1) I have main.cpp, rectangle.h and rectangle.cpp

2) Create a class called Rectangle in the rectangle cpp and header files.

3) In the Rectangle class there will be 2 private int variables called length and width

4) Also in the Rectangle class there will be public functions that will find and return the area. Also getter and setter functions




Question 1) The program will ask you to input the length. Next it will ask to input width. Then it will print the area on screen.
-Can someone show me the full code on how they would code this?




Thanks




Can someone show me the full code on how they would code this?

No (unless JLBorges decides to), and the reason why is that it happens too often that novices take the code given to them and run off with it, often without making an effort to understand it.

That said, you said you have main.cpp, rectangle.h, and rectangle.cpp. If you show us what they look like, maybe we can offer some advice on what to do next.

-Albatross
You can also browse http://www.cplusplus.com/doc/tutorial/ in hope that some of it is on topic.
rectangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// #include guard: https://en.wikipedia.org/wiki/Include_guard
#ifndef RECTANGLE_H
#define RECTANGLE_H

// define the class in the header file
class Rectangle {

    public:
        // note: this would also act as a default constructor
        // explicit: prevent mplicit conversion from int to Rectangle
        // https://en.cppreference.com/w/cpp/language/converting_constructor
        explicit Rectangle( int its_length = 0, int its_width = 0 ) ;

        // note: we do not declare other foundation operations like destructor
        // rule of zero: https://en.cppreference.com/w/cpp/language/rule_of_three#Rule_of_zero

        // public function that will find and return the area.
        // note: const https://isocpp.org/wiki/faq/const-correctness#const-member-fns
        // note: we return a long long to minimise the chance of an integer overflow
        // (length and width are of type int; length*width may be too large to be represented by int)
        long long its_area() const ;

        // getters (accessors) also const
        int its_length() const ;
        int its_width() const ;

        // setters (mutators) note: not const
        void its_length( int new_length ) ;
        void its_width( int new_width ) ;

    private:
        // there will be 2 private int variables called length and width
        int length; // invariant: length >= 0
        int width; // invariant: width >= 0
};

#endif // RECTANGLE_H 


rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// implement the class in the cpp file

#include "rectangle.h"

// note: we use a member initializer list to initialise he members
// https://en.wikipedia.org/wiki/Initialization_(programming)#Initializer_list
Rectangle::Rectangle( int its_length, int its_width ) : length(its_length), width(its_width)
{
     // the constructor is responsible for establishing the class invariant
     // here, our invariant is that the length and width are non-negative
     // in this simple example, we silently correct invalid values
     if( length < 0 ) length = 0 ;
     if( width < 0 ) width = 0 ;
}

int Rectangle::its_length() const { return length ; }
int Rectangle::its_width() const { return width ; }

// setters (mutators) note: the class is responsible for maintaining its invariant
void Rectangle::its_length( int new_length ) { length = new_length < 0 ? 0 : new_length ; }
void Rectangle::its_width( int new_width ) { width = new_width < 0 ? 0 : new_width ; }

long long Rectangle::its_area() const {

    // note: we want the multiplication to be done with long long,
    // so we convert length to a long long first
    const long long ll_length = its_length() ;

    // usual arithmetic conversions:
    // https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.cbclx01/cplr066.htm
    return ll_length * its_width() ;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "rectangle.h"
#include <iostream>

int main() {

    // The program will ask you to input the length.
    int length ;
    std::cout << "length: " ;
    std::cin >> length ;

    // Next it will ask to input width.
    int width ;
    std::cout << "width: " ;
    std::cin >> width ;

    // Then it will print the area on screen.

    // first, we create a rectangle with this width and height
    // we declare it as const since we never need to modify it
    const Rectangle rect( length, width ) ;

    // we get the area by calling its member function its_area()
    std::cout << "rectangle with width " << rect.its_width()
              << " and length " << rect.its_length()
              << " has an area " << rect.its_area() << '\n' ;
}
Topic archived. No new replies allowed.