Polymorphism. adding new functions

I have a base class Building. All its functions are virtual. Then I have a derived class named Residential Buildings. It overrides all virtual functions and has some additional data members and member functions. However, I encountered some problem while calling them in main function. I declare the Residential Building object r,then I create a pointer b of base class and it points to r. My problem is I can't access functions declared in child class (Residential Building).

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
 class Building {

protected:
	string materialUsed;
	int yearOfBuilding;
	string addressOfBuilding;

public:
	//Constructors and destructor of base class
	Building() {
		this->materialUsed = "N/A";
		this->yearOfBuilding = 0;
		this->addressOfBuilding = "N/A";
	}

	Building(string new_materialUsed,int new_yearOfBuilding,string new_addressOfBuilding)
	{
		this->materialUsed = new_materialUsed;
		this->yearOfBuilding = new_yearOfBuilding;
		this->addressOfBuilding = new_addressOfBuilding;
	}

	//end of constructors

	//Setters
	virtual void setMaterial(string new_material) = 0;
	virtual void setAgeOfBuilding(int new_age) = 0;
	virtual void setAdressOfBuilding(string new_adress) = 0;


	//Getters
	virtual string getMaterial() const = 0;
	virtual int getAge() const = 0;
	virtual string getAddress() const = 0;

};

class Residential_Building : public Building {

protected:
	int numberOfFloors;
	double area;

public:
	//Constructtors and destructor
	Residential_Building():Building() {
		this->area = 0.0;
		this->numberOfFloors = 0;
	}
	Residential_Building(string new_materialUsed,int new_yearOfBuilding,string new_addressOfBuilding,int new_numberOfFloors,double new_area):Building(new_materialUsed,new_yearOfBuilding,new_addressOfBuilding) {
		this->numberOfFloors = new_numberOfFloors;
		this->area = new_area;
	}



	//Setters
	void setMaterial(string new_material) {this->materialUsed = new_material;}
	void setAgeOfBuilding(int new_age) {this->yearOfBuilding = new_age;}
	void setAdressOfBuilding(string new_adress) {this->addressOfBuilding = new_adress;}
	void setNumberOfFloors(int new_NumberOfFloors) {this->numberOfFloors = new_NumberOfFloors;}
	void setArea(double new_area) {this->area = new_area;}
	
	//Getters
	string getMaterial() const {return this->materialUsed;}
	int getAge() const {return this->yearOfBuilding;}
	string getAddress() const {return this->addressOfBuilding;}
	int getNumberOfFloors() const {return this->numberOfFloors;}
	double getArea() const {return this->area;}
};

void main() {
	
	Residential_Building r;
	Building* b = &r;
}

I can't access all functions in Residential_Building class. Only those declared in Building class. What might be the problem? 
That's the point; when you use a Building* to refer to all of its derived types, you treat them all as Buildings, not whatever more specific type they were. If you could access Residential_Building methods, what would the program do if that Building* actually was pointing at a different derived class?
so the only solution would be to declare a Residential_Building and all member functions are gonna be accessible, right?
closed account (o3hC5Di1)
Hi there,

Let's look at your code:

1
2
3
4
5
void main() {
	
	Residential_Building r;
	Building* b = &r;
}


You create an object r of type Residential building. This object has all the possibilities your residential_building class defines. Because it's a "regular" type, as opposed to a pointer, they are accessible using the "member of" or "dot" operator, for example: r.getMaterial();.

For the sake of this explanation, let's say that an object of Building takes 4 bytes of memory, but the residential_building adds funcionality, so it requires 6 bytes of memory.

When you do Building* b = &r; you are telling the compiler: "I create a pointer to a building object, which is 4 bytes of size, and I set the pointer to point to the address of the residential_building instance i just created." Because you have told the compiler that "b" is pointing to a "Building", only 4 bytes in size, it will only be able to access the functionality present in those 4 bytes.

It's a bit simplified, but I hope that makes sense to you.

Let us know if you have any further questions.

All the best,
NwN
Topic archived. No new replies allowed.