mirror of
https://github.com/Light-City/CPlusPlusThings.git
synced 2026-05-12 19:07:24 +08:00
feat: 统一 cpp 文件编码格式为 utf-8
This commit is contained in:
@@ -13,21 +13,21 @@ class Figure{
|
||||
class Trianle:public Figure{
|
||||
public:
|
||||
void area(){
|
||||
cout<<"三角形面积:"<<x*y*0.5<<endl;
|
||||
cout<<"三角形面积:"<<x*y*0.5<<endl;
|
||||
}
|
||||
};
|
||||
class Rectangle:public Figure{
|
||||
public:
|
||||
void area(){
|
||||
cout<<"这是矩形,它的面积是:"<<x*y<<endl;
|
||||
cout<<"这是矩形,它的面积是:"<<x*y<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
//定义抽象类指针
|
||||
//定义抽象类指针
|
||||
Figure *pF=NULL;
|
||||
// Figure f1; 抽象类不能被实例化
|
||||
// Figure f1; 抽象类不能被实例化
|
||||
Rectangle r;
|
||||
Trianle t;
|
||||
t.set(10,20);
|
||||
@@ -36,7 +36,7 @@ int main(int argc, char const *argv[])
|
||||
r.set(10,20);
|
||||
pF=&r;
|
||||
pF->area();
|
||||
//定义抽象类引用
|
||||
//定义抽象类引用
|
||||
Figure &rF=t;
|
||||
rF.set(20,20);
|
||||
rF.area();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
|
||||
//Eg7-1.cpp
|
||||
//基类指针或引用指向派生类对象时,虚函数与非虚函数区别:
|
||||
//声明Employee的print为虚函数,则可访问到Manager的print函数,非虚函数,则只能访问到Employee的print
|
||||
//基类指针或引用指向派生类对象时,虚函数与非虚函数区别:
|
||||
//声明Employee的print为虚函数,则可访问到Manager的print函数,非虚函数,则只能访问到Employee的print
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
@@ -30,7 +30,7 @@ float Employee::getSalary(){
|
||||
return 0.0;
|
||||
}
|
||||
void Employee::print(){
|
||||
cout<<"姓名:"<<Name<<"\t"<<"编号:"<<Id<<endl;
|
||||
cout<<"姓名:"<<Name<<"\t"<<"编号:"<<Id<<endl;
|
||||
}
|
||||
|
||||
class Manager:public Employee{
|
||||
@@ -38,23 +38,23 @@ class Manager:public Employee{
|
||||
Manager(string name,string id,float s=0.0):Employee(name,id){
|
||||
weeklySalary=s;
|
||||
}
|
||||
void setSalary(float s) { weeklySalary=s; } //设置经理的周薪
|
||||
float getSalary(){ return weeklySalary; } //获取经理的周薪
|
||||
void print(){ //打印经理姓名、身份证、周薪
|
||||
cout<<"经理:"<<getName()<<"\t\t 编号: "<<getId()<<"\t\t 周工资: "<<getSalary()<<endl;
|
||||
void setSalary(float s) { weeklySalary=s; } //设置经理的周薪
|
||||
float getSalary(){ return weeklySalary; } //获取经理的周薪
|
||||
void print(){ //打印经理姓名、身份证、周薪
|
||||
cout<<"经理:"<<getName()<<"\t\t 编号: "<<getId()<<"\t\t 周工资: "<<getSalary()<<endl;
|
||||
}
|
||||
private:
|
||||
float weeklySalary; //周薪
|
||||
float weeklySalary; //周薪
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
不论哪种赋值方式,都只能通过基类对象(或基类对象的指针或引用)访问到派生类对象从基类中继承到的成员,
|
||||
不能借此访问派生类定义的成员。而虚函数使得可以通过基类对象的指针或引用访问派生类定义的成员。
|
||||
不论哪种赋值方式,都只能通过基类对象(或基类对象的指针或引用)访问到派生类对象从基类中继承到的成员,
|
||||
不能借此访问派生类定义的成员。而虚函数使得可以通过基类对象的指针或引用访问派生类定义的成员。
|
||||
*/
|
||||
int main(){
|
||||
Employee e("小米","NO0001"),*pM;
|
||||
Manager m("小汪","NO0002",128);
|
||||
Employee e("小米","NO0001"),*pM;
|
||||
Manager m("小汪","NO0002",128);
|
||||
m.print();
|
||||
pM=&m;
|
||||
pM->print();
|
||||
@@ -63,4 +63,4 @@ int main(){
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
//Virtual关键字其实质是告知编译系统,被指定为virtual的函数采用动态联编的形式编译。
|
||||
//Virtual关键字其实质是告知编译系统,被指定为virtual的函数采用动态联编的形式编译。
|
||||
|
||||
@@ -13,7 +13,7 @@ class C: public B {
|
||||
public:
|
||||
void f(int i){cout<<"C::f()"<<endl;}
|
||||
};
|
||||
//一旦将某个成员函数声明为虚函数后,它在继承体系中就永远为虚函数了
|
||||
//一旦将某个成员函数声明为虚函数后,它在继承体系中就永远为虚函数了
|
||||
class D: public C{
|
||||
public:
|
||||
void f (int){cout<<"D::f()"<<endl;}
|
||||
@@ -21,10 +21,10 @@ public:
|
||||
int main(){
|
||||
A *pA,a;
|
||||
B *pB, b; C c; D d;
|
||||
pA=&a; pA->f(1); //调用A::f
|
||||
pB=&b; pB->f(1); //调用B::f
|
||||
pB=&c; pB->f(1); //调用C::f
|
||||
pB=&d; pB->f(1); //调用D::f
|
||||
pA=&a; pA->f(1); //调用A::f
|
||||
pB=&b; pB->f(1); //调用B::f
|
||||
pB=&c; pB->f(1); //调用C::f
|
||||
pB=&d; pB->f(1); //调用D::f
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user