1

I have a two class called associate and report which is declared in associate.h file and report.h file respectively. report class inherits associate class in report.h as follows

associate.h

class associate {
public:
int num1, num2;
}

report.h

class report : public associate {
public:
int add();
}

I have a main.cpp where I have included associate.h header and created object and intialized values for the same like below.

associate a{};
a.num1 = 10;
a.num2 = 20;

now I have one more cpp file called report.cpp where i have included report.h and associate.h and trying to access num1 and num2 values like

report r{};
cout << r.num1 << num2 << endl;

which is actually not printing 10 and 20 which I have set in main.cpp. How to access those values from another cpp file in this case from report.cpp?

Vimalan E
  • 351
  • 3
  • 12
  • 1
    `a` has values for `num1` and `num2`, but `r` is a new instance where those members have not been initialized. – wally Apr 10 '20 at 13:28
  • @wally Please don't answer in comments; thanks – Asteroids With Wings Apr 10 '20 at 13:28
  • 1
    Just to add to what others wrote: this problem is not specific to inheritance. You have the same issue if you used `associate` in both cpp files - you're creating separate instances, what you to with one of them does not affect the other. You need to better understand the more basic OOP concepts before diving into inheritance. – Wutz Apr 10 '20 at 13:42
  • Use like this: `report* r = (report*)&a; cout << r->num1 << r->num2 << endl;` – seccpur Apr 10 '20 at 13:45
  • @seccpur No, don't use naked pointers. And don't put answers in comments. – Asteroids With Wings Apr 10 '20 at 18:51

2 Answers2

1

r and a are two different objects that occupy different extents of memory. So changing the object a has no effect on the object r.

If you want that the object r will get values of data members of the object a then you need to assign the object a to the sub-object of the type associate of the object r.

Here is a demonstrative program.

#include <iostream>

class associate {
public:
    int num1, num2;
};

class report : public associate {
public:
    int add();
};

int main() 
{
    associate a {};
    a.num1 = 10;
    a.num2 = 20;

    report r {};

    r.associate::operator =( a );

    std::cout << r.num1 << ' ' << r.num2 << std::endl;

    return 0;
}

Its output is

10 20

Another approach is as the class report is an aggregate then you can write

associate a{};
a.num1 = 10;
a.num2 = 20;

report r = { { a.num1, a.num2 } };
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You don't. You can't.

You've created two completely separate, unrelated objects. They're even of different types (one is an associate, and the other is a report; though in this case you're only interested in the common associate parts, so that's fine). One object does not contain the values from another, unrelated object.

We cannot see the structure of your program, but it seems to me that you need to be passing around pointers or references to your one object if you want to use it in multiple places. How exactly to do that depends on your program.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35
  • so I have to pass associate object to another file (report.cpp) in order to use it in multiple places ? – Vimalan E Apr 10 '20 at 13:32
  • geeksforgeeks.org and cppreference.com – Vimalan E Apr 10 '20 at 13:34
  • Each .cpp file is a translation unit, but they are all linked into a single program. There is no need to "pass" them between the files. – wally Apr 10 '20 at 13:37
  • 2
    @VimalanE Those are not books (and, though cppreference.com is good, it is a reference for people who already know the language). You need to learn C++ from a book if you want to succeed. [Here are some](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Asteroids With Wings Apr 10 '20 at 13:40
  • @wally You need to "pass" _something_, be it a pointer or reference or copy, between scopes (whether function scopes or whatever). Where those scopes reside lexically isn't really important. – Asteroids With Wings Apr 10 '20 at 13:41
  • @AsteroidsWithWings Exactly, but you can't answer 'yes' to the question of passing the object to another file. That doesn't make sense. – wally Apr 10 '20 at 13:42
  • @AsteroidsWithWings I agree that cppreference is not a tool for learning the language initially. At their apparent current level of understanding, the asker definitely needs a guided introduction to understand the basics. But they don't necessarily have to read a book. There are decent free introductions online. The problem is that there are more bad ones than good ones, and I don't have a recommendation on hand. – Wutz Apr 10 '20 at 13:50
  • @Wutz While I certainly agree that it is _possible_ to learn C++ without a book, a "safe" way to explain how to do that would involve weeding out all the rubbish on the internet, which is really just not feasible. And a proper narrative is always better than a smattering of "tutorials" written by people who barely know what they are doing. A peer-reviewed book from that list is always a safe bet, so for simplicity I usually just jump to that recommendation. Gosh, when did people become so afraid of _reading books_?? – Asteroids With Wings Apr 10 '20 at 13:59
  • @AsteroidsWithWings I was with you until the last sentence. It's not an aversion to reading books, I initially learned from a book myself. But especially for people learning their first programming language just to try it out, having to commit money and time to order a book before they can even start might turn them away. I don't think we have a real disagreement here, a good book is definitely the best way. I just wanted to point out that there are alternatives if you don't want to buy a book. – Wutz Apr 10 '20 at 14:25