classes constructors and overloading clarification

I am trying to learn a bit of c++ by following C+ How to program book.
I have got to a chapter on overloading and classes. I have written a very basic prgram and class with std::cout statements to try and get a better understanding of how things happen in c++.
I have a basic class with 2 constructors, 1 is the 'standard' constructor and the other uses an initiliser list. Each has a std::cout statement to show its call.
I have also written a destructor with does has a std::cout to show its call.
I overloaded the + operator and stream << as part of the exercise.
The bit where i need clarification is the line where the overloaded + is. Am I correct in thinking that the object is created inside the function and the variables are added then the object is returned as a paramter to the overloaded << ostream. Once displated it is then destroyed?

Thanks in advance
James


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  //
//  main.cpp
//  new,delete try on array objects
//
//  Created by James Farrow on 05/11/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#include <iostream>
#include "object.hpp"

int main() {
    
    std::cout << "5 objects in new array" << std::endl;
    object *array = new object[5];
    
    std::cout << "\nObject using initiliaser list " << std::endl;
    object test(5, 2);
    object test2(3, 4);
    std::cout << "Perimeter using member function of class" << std::endl;
    std::cout << "lenght * Breadth: " << test.perimeter()  << std::endl;
    
    std::cout << "\nUsing for loop to call member function of objects in array" << std::endl;
    for (int i = 0; i < 5; i++) {
        array[i].printSomething();
    }
    std::cout << std::endl;
    
    std::cout << "addition of 2 objects using overloaded +\n" << test + test2 << std::endl;
    
    
    std::cout << "Desructors called at end of main" << std::endl;
    delete [] array;
    
    return 0;
}

//
//  object.hpp
//  new,delete try on array objects
//
//  Created by James Farrow on 05/11/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#ifndef object_hpp
#define object_hpp
#include <iostream>

#include <stdio.h>

class object{
    friend std::ostream &operator<<(std::ostream& out, object const &rhs);
public:
    object();
    object( int, int );
    ~object();
    void printSomething();
    int perimeter();
    object operator+(const object &other ) const;
private:
    int length;
    int breadth;
};

#endif /* object_hpp */

//
//  object.cpp
//  new,delete try on array objects
//
//  Created by James Farrow on 05/11/2016.
//  Copyright © 2016 James Farrow. All rights reserved.
//

#include "object.hpp"
#include <iostream>

object::object( int len, int bred): length(len), breadth(bred){
    std::cout << "constructor" << std::endl;
}

object::object(){
    std::cout << "standard constructor" << std::endl;
}

object::~object(){
    std::cout << "destructor" << std::endl;
}

void object::printSomething(){
    std::cout << "print something" << std::endl;
}

int object::perimeter(){
    return length * breadth;
}

object object::operator+(const object &other) const {
    
    object result;
    
    int templength = this->length + other.length;
    int tempbreadth = this->breadth + other.breadth;
    
    result.length = templength;
    result.breadth  =tempbreadth;
    
    return result;
}

std::ostream &operator<<( std::ostream& out, object const &rhs){
    out << "length: " << rhs.length << " " << "breadth: " << rhs.breadth;
    return out;
}
Last edited on
Just realised the function perimeter should actually be called area, but for this it doesn't matter...

James
1. Your constructor needs to be set.. Everything in it is empty
object::object(){
length = 0;
breadth = 0;
// you also want to create your new object in here.
std::cout << "constructor" << std::endl;
}

2. The purpose of the destructor is to clear all data when the program ends.
object::~object(){
delete [] array;
std::cout << "destructor" << std::endl;
}

3. Yes you are correct about the operator overload. You have not stored the return value.
std::cout << "addition of 2 objects using overloaded +\n" << test + test2 << std::endl;

You are saying that :
std::cout << "Add two numbers " << 3 + 5 << std::endl;

if you don't store the sum then it is deleted after it is called.


*** instead of calling "std" all the time. make it global **
"using namespace std;"
Last edited on
Topic archived. No new replies allowed.