问题在代码注释中,万分谢谢!如果能有针对性的提供参考资料或者书籍更好,谢谢!
demo.h
class A
{
protected:
int age;
public:
virtual void print();
A(int ); //问题一、函数参数只有类型没有函数参数?
~A();
};
class B : public A //问题二、冒号是什么意思?
{
public:
void print(); //问题三、print是哪个类型函数,不是构造函数吧?
//问题四,如果头文件引用了C的<stdio.h>,会冲突不?
B(int);
~B();
};
class C
{
private:
A a;
public:
C(A temp); //问题五、A temp 是什么?A是变量类型,temp是变量?
~C();
};
demo.cpp //问题六此文件的函数都是公开的还是私有的
A::A(int t):age(t)
{
cout << "constructor A!" << endl;
}
void A::print()
{
cout << "print A!" << endl;
}
A::~A()
{
cout << "destructor A!" << endl;
}
B::B(int t):A(t) //问题七、冒号是什么意思
{
cout << "constructor B!" << endl;
}
void B::print()
{
cout << "print B!" << endl;
}
B::~B()
{
cout << "destructor B!" << endl;
}
C::C(A temp):a(temp)
{
cout << "constructor C!" << endl;
}
C::~C()
{
cout << "destructor C!" << endl;
}
class A
{
protected:
int age;
public:
virtual void print();
A(int ); //问题一、函数声明值需要参数类型,不需要参数名字。在函数实现的时候需要参数名字,在函数体里用名字访问值
~A();
};
class B : public A //问题二、冒号是继承A类的意思
{
public:
void print(); //问题三、print是普通函数,不是构造函数吧。构造函数名字试类名。
//问题四,如果头文件引用了C的<stdio.h>,不会冲突,因为头文件里一般都有条件控制 #if defined ...
B(int);
~B();
};
class C
{
private:
A a;
public:
C(A temp); //问题五、A temp就时参数类型和名字,A是变量类型,temp是变量
~C();
};
A::A(int t):age(t)
{
cout << "constructor A!" << endl;
}
void A::print()
{
cout << "print A!" << endl;
}
A::~A()
{
cout << "destructor A!" << endl;
}
B::B(int t):A(t) //问题七、冒号是调用父类A的构造函数,在进入B的构造函数体之前执行,这样保证在子类构造函数里父类已经初始化完成
{
cout << "constructor B!" << endl;
}
void B::print()
{
cout << "print B!" << endl;
}
B::~B()
{
cout << "destructor B!" << endl;
}
C::C(A temp):a(temp)
{
cout << "constructor C!" << endl;
}
C::~C()
{
cout << "destructor C!" << endl;
}
可以全面学习我的一个C和C++专栏,望采纳,不懂的可以关注私信我。