第6章 继承与派生

1。继承的作用是什么?
   它可以允许不同的类以多种不同的方法共享代码。
2。解释多态,什么是多态?如何使用多态。
3。解释动态绑定与静态绑定的区别?描述各自的试能条件。
   动态绑定:即运行的时候的绑定,多态就是这样的。解决了运行的时候方法的间接调用。
   静态绑定:针对方法的直接调用(不通过指针或者引用)仍然是由编译器进行解析的。
4。如何覆盖基类的一个方法?
   当基类中的一个虚函数在派生类中也存在并且函数签名相同时,我们就说派生类中的版本覆盖了基类中的定义。
5。什么是纯虚函数?其主要用途是什么?
6。什么是抽象类?它有什么用途?有什么操作可以针对实体类进行却不能针对抽象类进行?
7。某个基类函数被隐常的含义是什么?什么可以引起此种情况的发生?
8。哪些成员函数无法从基类函数进行继承?解释其原因。
   ①拷贝构造函数②拷贝赋值函数③析构函数
   如果类没有明确定义拷贝构造函数和拷贝赋值函数,编译器会默认为这个类生成。
   编译器也会默认为析构生成一个默认的析构函数。

#include <QTextStream>
#include "student.h"

Student::Student(QString nm, long id,QString major,int year)
:m_Name(nm)
,m_Major(major)
,m_StudentId(id)
,m_Year(year){}

QString Student::getClassName() const
{
 return "Student";
}

QString Student::toString() const
{
 QString retval;
 QTextStream os(&retval);
 
 os << "[" << getClassName() << "]"
  << " name:" << m_Name
  << " id:" << m_StudentId
  << " Major:" << m_Major;
 return retval;
}

Undergrad::Undergrad(QString name, long id,QString major,int year)
:Student(name, id, major, year){}

QString Undergrad::getClassName() const
{
 return "Undergrad";
}

GradStudent::GradStudent(QString name, long id, QString major, int year, GradStudent::Support support)
:Student(name,id,major,year),
m_Support(support){}


QString GradStudent::toString() const
{
 QString result;
 QTextStream os(&result);
 os << Student::toString()
  << supportStr(m_Support)
  << "/n";
 return result;
}

QString GradStudent::getClassName() const
{
 return "GradStudent";
}

QString GradStudent::supportStr(Support support)
{
 return "test";
}

 

#include <QApplication>
#include <QTextStream>
#include  "student.h"

static QTextStream cout(stdout,QIODevice::WriteOnly);

void graduate (Student* student);

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 
 Undergrad us("Frodo",5562,"Ring Theory", 4);
 GradStudent gs("Bilbo",3029,"history",6,GradStudent::fellowship);
 cout << "Here is the data for the two students:/n";
 cout << gs.toString() << endl;
 cout << us.toString() << endl;
 cout << "/nHere is what happens when they graduate:/n";

 graduate(&us);
 graduate(&gs);

 return a.exec();
}

void graduate (Student* student)
{
 cout << "/nTHE FOLLOWING"
  << student->getClassName()
  << "has graduated/n"
  << student->toString() << "/n";
}

 

 

#include <QString>
/* duo tai pai sheng*/
class Student{
public:
 Student(QString nm, long id, QString m_Major, int year = 1);
 virtual QString getClassName() const;/*it has been uesd in sub class*/
 QString toString() const;/*In sub class, it will be spread*/
 virtual ~Student(){}
 
private:
 QString m_Name;
 QString m_Major;
 long m_StudentId;
protected:
 int m_Year;

};

class Undergrad : public Student {
public:
 Undergrad(QString name, long id, QString major, int year);
 QString getClassName() const;
};

class GradStudent : public Student {
public:
 enum Support {ta, ra, fellowship, other};
 GradStudent(QString name, long id, QString major, int year, Support support);
 QString getClassName() const;
 QString toString() const;
protected:
 static QString supportStr(Support sup);
private:
 Support m_Support;
};

多态派生6-8

#include <QtCore/QCoreApplication>

#include <iostream>
using namespace std;

class A{
public:
 virtual void foo(){
  cout << "A's foo" << endl;
 }
 virtual void bar(){
  cout << "A's bar" << endl;
 }

};

class B:public A{
public:
 void foo(){
  cout << "B's foo" << endl;
 }
 void bar(){
  cout << "B's bar" << endl;
 }
};

int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 B bobj;
 A *aptr = &bobj;
 aptr->foo();/*B's foo*/
 A aobj = *aptr;
 aobj.foo();/*A's foo*/

 return a.exec();
}

 

多态派生6-9

#include <QtCore/QCoreApplication>

#include <iostream>
using namespace std;

class A{
public:
 virtual void foo(){
  cout << "A's foo" << endl;
 }
};

class B:public A{
public:
 void foo(){
  cout << "B's foo" << endl;
 }
};

class C:public B{
public:
 void foo(){
  cout << "C's foo" << endl;
 }
};

int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 C cobj;
 B *bptr = &cobj;
 bptr->foo();/*C's foo*/
 A *aptr = &cobj;
 bptr->foo();/*C's foo*/

 return a.exec();
}

 

多态派生6-10

base.h

#include <iostream>
using namespace std;
class Base{
public:
 Base();
 void a();
 virtual void b();
 virtual void c(bool condition = true);
 virtual ~Base(){};
};

class Derived : public Base {
public:
 Derived();
 virtual void a();
 void b();
 void c();
};

base.cpp

#include "base.h"

Base::Base(){
 cout << "Base::Base" << endl;
 //a();
 //c();
}

void Base::c(bool condition){
 cout << "Base::c" << endl;
}

void Base::a(){
 cout << "Base::a" << endl;
 //b();
}

void Base::b(){
 cout << "Base::b" << endl;
}

Derived::Derived(){
 cout << "Derived::Derived" << endl;
}

void Derived::a(){
 cout << "Derived::a" << endl;
 //c();
}

void Derived::b(){
 cout << "Derived::b" << endl;
}

void Derived::c(){
 cout << "Derived::c" << endl;
}

#include <QtCore/QCoreApplication>
#include <iostream>
#include "base.h"
using namespace std;
int main (int argc, char** argv){
 
 QCoreApplication a(argc, argv);
 
 Base b;
 Derived d;

 cout << "Objects Created" << endl;
 b.b();/*Derived::b*/
 cout << "Calling derived methods" << endl;
 d.a();/*Derived::a*/
 d.b();/*Derived::b*/
 d.c();/*Derived::c*/
 cout << ".. via base class pointers..." << endl;
 Base* bp = &d;
 bp->a();/*base::a*/
 bp->b();/*Derived::b*/
 bp->c();/*base::c*/

 return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值