第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();
}

你可能感兴趣的:(c,OS,Class,编译器,methods)