注:最后有面试挑战,看看自己掌握了吗
#include
struct point
{
int x;
int y;
void output()
{
std::cout<<x<<std::endl<<y;
}
};
void main()
{
point pt;
pt.x=0;
pt.y=0;
pt.output();
}
#include
using namespace std;
class point
{
public:
int x;
int y;
point()
{
x=0;
y=0;
}
point(int a,int b)
{
x=a;
y=b;
}
void output()
{
cout<<x<<endl<<y<<endl;
}
};
void main()
{
point pt(5,5);
pt.output();
point cc;
cc.output();
}
#include
using namespace std;
class Student
{
private:
char* pName;
public:
Student()
{
pName = new char[20];
}
~Student()
{
delete[] pName;
}
};
#include"标头.h"
using namespace std;
class animal
{
public:
void eat()
{
cout<<"animal eat"<<endl;
}
void sleep()
{
cout<<"animal sleep"<<endl;
}
void breathe()
{
cout<<"animal breathe"<<endl;
}
};
class fish:public animal
{
};
class dog :public animal
{
};
void main()
{
animal an;
fish fh;
an.breathe();
fh.breathe();
dog dd;
dd.breathe();
}
#include
using namespace std;
class B1
{
public:
void output();
};
class B2
{
public:
void output();
};
void B1::output()
{
cout<<"call the class B1"<<endl;
}
void B2::output()
{
cout<<"call the class B2"<<endl;
}
class A:public B1,public B2
{
public:
void show();
};
void A::show()
{
cout<<"call the class A"<<endl;
}
void main()
{
A a;
//a.output(); //该语句编译时会报错
a.show();
}
animal *pAn;
fish fh;
pAn=&fh;
#include
using namespace std;
class animal
{
public:
void eat()
{
cout<<"animal eat"<<endl;
}
void sleep()
{
cout<<"animal sleep"<<endl;
}
virtual void breathe()
{
cout<<"animal breathe"<<endl;
}
};
class fish:public animal
{
public:
void breathe()
{
cout<<"fish bubble"<<endl;
}
};
void fn(animal *pAn)
{
pAn->breathe();
}
void main()
{
animal *pAn;
fish fh;
pAn=&fh;
fn(pAn);
}//结果是fish bubble
virtual void breathe() = 0;
class <类名>
{
virtual <类型><函数名>(<参数表>)=0;
…
};
隐藏:当父类函数与派生类函数同名,并且父类函数无 virtual关键字修饰,无论父类与派生类参数个数与类型是否相同,此时派生类函数隐藏父类所有同名函数
覆写:当父类函数与派生类函数同名,并且参数相同,返回值相同,并且父类函数有 virtual关键字修饰,此时派生类函数覆写父类函数
int a=5;
int &b=a;
void f(int& a, int &b)
{
}
#ifndef XXX//名字随便取,一般有实际意义 比如ANIMAL_H
#define XXX
class animal
{
public:
animal();
~animal();
void eat();
void sleep();
virtual void breathe();
};
class doge
{
};
#endif // !XXX


🍃博主昵称:
一拳必胜客
特别鸣谢:木芯工作室 、Ivan from Russia