Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 07e4991

Browse files
Demos
1 parent dbb3009 commit 07e4991

File tree

4 files changed

+136
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 14 Base Class Pointer Derived Class Object

4 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Base
6+
{
7+
public:
8+
void function1();
9+
void function2();
10+
void function3();
11+
};
12+
13+
14+
class Derived : public Base
15+
{
16+
public:
17+
void function4();
18+
void function5();
19+
};
20+
21+
int main(void)
22+
{
23+
Base *ptr;
24+
ptr = new Derived();
25+
26+
ptr ->function1();
27+
ptr ->function2();
28+
ptr ->function3();
29+
30+
Derived *ptrd;
31+
// ptrd = new Base(); // this is not correct -> the specialized class pointer can not be represented by an object of type Base as this object does not have all the specialized functions
32+
33+
return 0;
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Base
6+
{
7+
public:
8+
void function1()
9+
{
10+
cout<<"Function1 called here"<<endl;
11+
}
12+
};
13+
14+
class Derived: public Base
15+
{
16+
public:
17+
void function2()
18+
{
19+
cout<<"Function2 called here"<<endl;
20+
}
21+
};
22+
23+
int main(void)
24+
{
25+
Base *ptr;
26+
ptr->function1();
27+
//ptr->function2();
28+
29+
return 0;
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Rectangle
6+
{
7+
public:
8+
void area()
9+
{
10+
cout<<"Area function here"<<endl;
11+
}
12+
};
13+
14+
class Cuboid: public Rectangle
15+
{
16+
public:
17+
void volume()
18+
{
19+
cout<<"Volume function here"<<endl;
20+
}
21+
};
22+
23+
24+
int main(void)
25+
{
26+
//Rectangle r;
27+
//Cuboid *ptrc = &r; // this is not correct an object of type base does not know about all the extended functionality for the cuboid
28+
//ptrc ->area();
29+
30+
Cuboid c;
31+
Rectangle *ptrr = &c;
32+
ptrr->area();
33+
//ptrr->volume(); this pointer does not know about the volume() function , it expects to find a Rectangle where it is pointing to
34+
35+
return 0;
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class BaseCar
6+
{
7+
public:
8+
void start()
9+
{
10+
cout<<"Start car"<<endl;
11+
}
12+
};
13+
14+
class AdvancedCar: public BaseCar
15+
{
16+
public:
17+
void playMusic()
18+
{
19+
cout<<"Play music"<<endl;
20+
}
21+
};
22+
23+
int main(void)
24+
{
25+
AdvancedCar c;
26+
BaseCar *ptr=&c;
27+
28+
ptr->start();
29+
//ptr->playMusic();
30+
31+
32+
BaseCar car;
33+
//AdvancedCar *ptra = &car; // this can not be done
34+
35+
return 0;
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /