The textbooks I bought haven’t arrived...it’s boring to watch the discussion between the younger students and the younger sisters in the C++ class. Let’s brush up the questions to consolidate them~
Today’s exercises are https://wenku.baidu.com/view/7232fb0f7cd184254b353523.html
Virtual function
It is declared as virtual in the base class, and the member function that is redefined in the derived class
(abstract function in Java)
defines a function as a pure virtual function , which means that the function has not been implemented
. Dynamic binding is realized . The virtual function passes the object pointer transfer
virtual void funtion1()=0//纯虚函数
The class that contains pure virtual functions is an abstract class, and it is not possible to define object
= 0. The essence is to set the pointer to the function body as NULL.
There must be a redefined pure virtual function body in the derived class, so that the derived class can be used to define the object
Static polymorphism + dynamic polymorphism
Static polymorphism : function overloading,
dynamic polymorphism : use inheritance + encapsulation, virtual function
Member function overload operator
下列关于运算符重载的叙述中,正确的是:B
A. 通过运算符重载可以定义新的运算符
B. 有的运算符只能作为成员函数重载
C. 若重载运算符+,则相应的运算符函数名是+
D. 重载二元运算符时,必须声明两个形参
B. =, [], (), -> can only be overloaded through member functions
C. The function name of the overloaded operator is operator + operator
D. When the overloaded binary operator is a member function, the function has only one Formal parameter , another parameter is implicitly the object itself
在表达式x+y*z中,+是作为成员函数重载的运算符,*是作为非成员函数重载的运算符,则operator+有___个参数,operator*有___个参数
//1,2
Member function overload operator: the first parameter is the caller
Non-member function overload operator: the operands are all in the parameters
The operator overload function may be a member function of a class, a friend function of a class, or a normal function
Inheritance protected
class MyBASE {
private:int k;
public:
void set(int n) {
k=n;}
int get() const {
return k;}}
class MyDERIVED:protected MyBase {
protected :int j;
public:
void set(int m,int n){
MyBASE::set(m);j=n;}
int get() const {
return MyBASE::get()+j;}}
When inherited is protected, the public part of the entire parent class is protected.
So the number of data members and member functions protected by MyDERIVED is: 3 (set(int n), get(), int j)
The calling sequence of the constructor
Parent class constructor -> object member constructor -> subclass constructor
(destructuring is the reverse)
class A {
public:A()<cout<<"A"}}
class A {
public:B()<cout<<"B"}}
class C:public A
{
B b;
public: C(){
cout<<"C";}}
int main()
{
C obj;
return 0;}
//输出:ABC