인프런 커뮤니티 질문&답변
m_i 값이 안바뀌는 이유가 뭔가요?
작성
·
345
1
//
// main.cpp
// inheritance_ex01
//
// Created by Kevin on 04/10/2019.
// Copyright © 2019 Kevin. All rights reserved.
//
#include <iostream>
using namespace std;
class Mother { // basic class
private:
int m_i;
public:
//Mother() {}
Mother(const int& i_in) : m_i(i_in) {
std::cout << "Mother constructor" << std::endl;
std::cout << "m_i " << m_i << std::endl;
}
void setValue(const int& i_in) {
std::cout << "i_in " << i_in << std::endl;
m_i = i_in;
std::cout << "m_i " << m_i << std::endl;
}
int getValue() {
return m_i;
}
};
class Child : public Mother {
private:
double m_d;
public:
Child(const int& i_in, const double& d_in) : Mother(i_in), m_d(d_in) {
std::cout << "Child constructor" << std::endl;
Mother::setValue(i_in);
}
void setValue(const int& i_in, const double& d_in) {
}
void setValue(const double& d_in) {
m_d = d_in;
}
double getValue() {
return m_d;
}
}; // derived class
int main(int argc, const char * argv[]) {
Mother mother(512);
std::cout << mother.getValue() << std::endl;
Child child(1024, 2048);
std::cout << "m_i value : " << mother.getValue() << std::endl;
// 왜 512인가요?
std::cout << "m_d value : " << child.getValue() << std::endl;
return 0;
}
답변 4
1
김영성
질문자
원인은 찾았는데, 개념적인 이해를 이렇게 해도 될까요?
mother에서 상속받은 m_i는 mother의 m_i를 상속 받으면, child에 별개의 상속받은 m_i가 있다고 보면 되는 것인가요?
상속을 받으면, 동일한 m_i를 공유 한다고 생각을 했었습니다.
0
0
0





