-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchap8pe3.cpp
73 lines (67 loc) · 1.34 KB
/
chap8pe3.cpp
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
#include <iostream>
using namespace std;
// base class
class person {
//private members
private:
string name;
int age;
// protected member
protected:
int roll_no;
//public members
public:
void getPerson()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
void dispPerson()
{
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
// derived class
class person1 : public person {
// private members
private:
string city;
// public members
public:
void set_roll_no(int r)
{
// here roll_no is the protected member of person
// class, it is accessible here
roll_no = r;
}
void getPerson1()
{
// calling getPerson() to read basic details
getPerson();
// input city
cout << "Enter city: ";
cin >> city;
}
void dispPerson1()
{
// displaying roll_no
cout << "Roll No.: " << roll_no << endl;
// calling dispPerson() to print basic details
dispPerson();
// displaying city also
cout << "City: " << city << endl;
}
};
// main function
int main()
{
//creating object
person1 per;
per.set_roll_no(101);
per.getPerson1();
per.dispPerson1();
return 0;
}