day07_类的声明
声明和实现分离
#pragma once
// 声明 .h 头文件
class Person {
int m_age;
public:
Person();
~Person();
void setAge(int age);
int getAge();
};
#include "Person.h"
#include <iostream>
using namespace std;
// ::是域运算符
// 实现 .cpp 源文件
Person::Person() {
cout << "Person()" << endl;
}
Person::~Person() {
cout << "~Person()" << endl;
}
void Person::setAge(int age) {
this->m_age = age;
}
int Person::getAge() {
return this->m_age;
}
命名空间
命名空间可以用来避免命名冲突
命名空间不影响内存布局
#include <iostream>
#include "Car.h"
#include "Person.h"
//using namespace KP;
using namespace std;
void test() {
cout << "test()" << endl;
}
namespace FX {
int g_no;
class Person {
public:
int m_age;
};
void test() {
cout << "FX::test()" << endl;
}
}
//
namespace KP {
int g_no;
class Person {
public:
int m_height;
};
void test() {
cout << "KP::test()" << endl;
}
}
void test1() {
FX::g_no = 1;
KP::g_no = 2;
FX::Person *p1 = new FX::Person();
p1->m_age = 10;
KP::Person *p2 = new MJ::Person();
p2->m_height = 180;
test();
FX::test();
KP::test();
}
namespace KP {
int g_no;
class Person {
public:
int m_height;
};
void test() {
cout << "MJ::test()" << endl;
}
}
void test2() {
/*using namespace MJ;
g_no = 10;
Person person;
test();*/
/*using MJ::g_no;
using MJ::Person;
g_no = 10;
Person person;*/
}
namespace KP {
namespace SS {
int g_no;
class Person {
};
void test() {
}
}
void test() {
}
}
void test() {
}
void test3() {
test();
// 默认的命名空间,没有名字
::test();
::KP::SS::g_no = 30;
KP::SS::g_no = 20;
using namespace KP;
SS::g_no = 30;
using namespace KP::SS;
g_no = 10;
}
int main() {
KP::Car car;
KP::Person person;
getchar();
return 0;
}
继承
父类的成员变量在前,子类的成员变量在后
//struct Person {
// int m_age;
// void run() {
// cout << "run()" << endl;
// }
//};
//
//struct Student : Person {
// int m_score;
// void study() {
// cout << "study()" << endl;
// }
//};
//
//struct Worker : Person {
// int m_salary;
// void work() {
// cout << "work()" << endl;
// }
//};
void test() {
/*Student student;
student.m_age = 18;
student.m_score = 100;
student.run();
student.study();*/
}
struct Person {
int m_age;
};
struct Student : Person {
int m_no;
};
struct GoodStudent : Student {
int m_money;
};
int main() {
// 12
GoodStudent gs;
gs.m_age = 20;
gs.m_no = 1;
gs.m_money = 2000;
cout << &gs << endl;
cout << &gs.m_age << endl;
cout << &gs.m_no << endl;
cout << &gs.m_money << endl;
// 4
Person person;
// 8
Student stu;
cout << sizeof(Person) << endl;
cout << sizeof(Student) << endl;
cout << sizeof(GoodStudent) << endl;
getchar();
return 0;
}
成员访问权限
- 成员访问权限、继承方式有3种
- public:公共的,任何地方都可以访问(struct默认)
- protected:子类内部、当前类内部可以访问
- prvate:私有的,只有当前类内部可以访问(class默认)
- 子类内部访问父类成员的权限,是以下2项中权限最小的那个
- 成员本身的访问权限
- 上一级父类的继承方式
- 开发中用的最多的继承方式是public,这样能保留父类原来的成员访问权限
- 访问权限不影响对象的内存布局
class Person {
public:
int m_age;
void run() {
}
};
class Student : public Person {
int m_no;
void study() {
m_age = 10;
}
};
class GoodStudent : public Student {
int m_money;
void work() {
m_age = 10;
}
};
class Person {
private:
int m_age;
public:
int m_no;
};
int main() {
Person person;
person.m_no = 20;
getchar();
return 0;
}