mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
修改注释的位置
This commit is contained in:
parent
38c4b4679e
commit
66094d9a13
38
README.md
38
README.md
|
@ -234,7 +234,7 @@ struct Student {
|
||||||
int age;
|
int age;
|
||||||
};
|
};
|
||||||
|
|
||||||
void f( Student me ); // 正确,"struct" 关键字可省略
|
void f( Student me ); // 正确,"struct" 关键字可省略
|
||||||
```
|
```
|
||||||
|
|
||||||
二、若定义了与 `Student` 同名函数之后,则 `Student` 只代表函数,不代表结构体,如下:
|
二、若定义了与 `Student` 同名函数之后,则 `Student` 只代表函数,不代表结构体,如下:
|
||||||
|
@ -244,13 +244,13 @@ typedef struct Student {
|
||||||
int age;
|
int age;
|
||||||
} S;
|
} S;
|
||||||
|
|
||||||
void Student() {} // 正确,定义后 "Student" 只代表此函数
|
void Student() {} // 正确,定义后 "Student" 只代表此函数
|
||||||
|
|
||||||
//void S() {} // 错误,符号 "S" 已经被定义为一个 "struct Student" 的别名
|
//void S() {} // 错误,符号 "S" 已经被定义为一个 "struct Student" 的别名
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
Student();
|
Student();
|
||||||
struct Student me; // 或者 "S me";
|
struct Student me; // 或者 "S me";
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -454,8 +454,8 @@ int main()
|
||||||
{
|
{
|
||||||
Shape * shape1 = new Circle(4.0);
|
Shape * shape1 = new Circle(4.0);
|
||||||
Shape * shape2 = new Rect(5.0, 6.0);
|
Shape * shape2 = new Rect(5.0, 6.0);
|
||||||
shape1->calcArea(); //调用圆形类里面的方法
|
shape1->calcArea(); //调用圆形类里面的方法
|
||||||
shape2->calcArea(); //调用矩形类里面的方法
|
shape2->calcArea(); //调用矩形类里面的方法
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -466,11 +466,11 @@ int main()
|
||||||
class Shape
|
class Shape
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Shape(); //构造函数不能是虚函数
|
Shape(); //构造函数不能是虚函数
|
||||||
virtual double calcArea();
|
virtual double calcArea();
|
||||||
virtual ~Shape(); //虚析构函数
|
virtual ~Shape(); //虚析构函数
|
||||||
}
|
}
|
||||||
class Circle : public Shape //圆形类
|
class Circle : public Shape //圆形类
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual double calcArea();
|
virtual double calcArea();
|
||||||
|
@ -480,7 +480,7 @@ int main()
|
||||||
{
|
{
|
||||||
Shape * shape1 = new Circle(4.0);
|
Shape * shape1 = new Circle(4.0);
|
||||||
shape1->calcArea();
|
shape1->calcArea();
|
||||||
delete shape1; //因为是虚析构函数,所以调用子类析构函数后,也调用父类析构函数。
|
delete shape1; //因为是虚析构函数,所以调用子类析构函数后,也调用父类析构函数。
|
||||||
shape1 = NULL;
|
shape1 = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -547,8 +547,8 @@ p = nullptr;
|
||||||
```cpp
|
```cpp
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
T* t = new T(); // 先内存分配 ,再构造函数
|
T* t = new T(); // 先内存分配 ,再构造函数
|
||||||
delete t; // 先析构函数,再内存释放
|
delete t; // 先析构函数,再内存释放
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -579,20 +579,20 @@ std::auto_ptr<std::string> ps (new std::string(str));
|
||||||
class Flyable //【能飞的】
|
class Flyable //【能飞的】
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual void takeoff() = 0; //起飞
|
virtual void takeoff() = 0; // 起飞
|
||||||
virtual void land() = 0; //降落
|
virtual void land() = 0; // 降落
|
||||||
}
|
}
|
||||||
class Bird : public Flyable //【鸟】
|
class Bird : public Flyable //【鸟】
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void foraging() {...} //觅食
|
void foraging() {...} // 觅食
|
||||||
virtual void takeoff() {...}
|
virtual void takeoff() {...}
|
||||||
virtual void land() {...}
|
virtual void land() {...}
|
||||||
}
|
}
|
||||||
class Plane : public Flyable //【飞机】
|
class Plane : public Flyable //【飞机】
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void carry() {...} //运输
|
void carry() {...} // 运输
|
||||||
virtual void take off() {...}
|
virtual void take off() {...}
|
||||||
virtual void land() {...}
|
virtual void land() {...}
|
||||||
}
|
}
|
||||||
|
@ -609,13 +609,13 @@ private:
|
||||||
...
|
...
|
||||||
}
|
}
|
||||||
|
|
||||||
class doSomething(Flyable *obj) //【做些事情】
|
class doSomething(Flyable *obj) //【做些事情】
|
||||||
{
|
{
|
||||||
obj->takeoff();
|
obj->takeoff();
|
||||||
|
|
||||||
cout << typeid(*obj).name() << endl; //输出传入对象类型(Bird or Plane)
|
cout << typeid(*obj).name() << endl; //输出传入对象类型(Bird or Plane)
|
||||||
|
|
||||||
if(typeid(*obj) == typeid(Bird)) //判断对象类型
|
if(typeid(*obj) == typeid(Bird)) //判断对象类型
|
||||||
{
|
{
|
||||||
Bird *bird = dynamic_cast<Bird *>(obj); //对象转化
|
Bird *bird = dynamic_cast<Bird *>(obj); //对象转化
|
||||||
bird->foraging();
|
bird->foraging();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user