-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_03_class.cpp
More file actions
47 lines (37 loc) · 1010 Bytes
/
_03_class.cpp
File metadata and controls
47 lines (37 loc) · 1010 Bytes
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
#include<iostream>
#include<cstring>
using namespace std;
class Person{
//必须声明为public,否则子类不可访问父类构造方法
public:
Person(string name):name(name),age(-1){};
Person(string name,int age):name(name),age(age) {};
void printInfo(){
cout<<"Person : ( name : "<<this->name<<" , age : "<<this->age<<" )"<<endl;
}
string getName(){
return this->name;
}
int getAge(){
return this->age;
}
private:
string name;
int age;
};
//默认class是 私有继承 class中的变量也是私有的
class Student:public Person {
public:
Student(string name,int age,double score):Person(name,age){
this->score = score;
};
void printInfo(){
cout<<"Student : ( name : "<<this->getName()<<" , age : "<<this->getAge()<<" , score : "<<this->score<<" )"<<endl;
}
private:
double score;
};
int main(int argc, char* argv[]){
Student s("小明",10,98);
s.printInfo();
}