基于一份示例代码的基础上来总结
#include <iostream>
#include <string>
class Date
{
public:
Date(unsigned int nYear , unsigned int nMonth , unsigned int nDay );
virtual ~Date();
void SetYear(unsigned int nYear);
void SetMonth(unsigned int nMonth);
void SetDay(unsigned int nDay);
unsigned int GetYear();
unsigned int GetMonth();
unsigned int GetDay();
private:
unsigned int m_Year;
unsigned int m_Month;
unsigned int m_Day;
};
class People
{
public:
//一个类可以有多个构造函数
People(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay , unsigned int nId);
People();
virtual ~People();
//拷贝构造函数,主要用于复制一个一样的对象
People( const People &nObj );
void SetNumber(unsigned int nNumber);
void SetSex(bool nSex);
void SetBirthday(unsigned int nYear , unsigned int nMonth , unsigned int nDay);
void SetId(unsigned int nId);
void Display();
private:
unsigned int m_Number;
bool m_Sex;
Date *m_Birthday;
unsigned int m_Id;
//protected属性的意义
protected:
//同名变量和函数在基类和派生类以及外部实现时,如何区分使用
int test;
};
class Student : public People
{
public:
Student(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay , unsigned int nId);
~Student();
void ChangeTest(float nTest);
void DisplayVal();
private:
float test;
};
#include "test.h"
Date::Date(unsigned int nYear , unsigned int nMonth , unsigned int nDay)
{
m_Year = nYear;
m_Month = nMonth;
m_Day = nDay;
}
Date::~Date()
{
}
//同样的函数能否简化实现方法
void Date::SetYear(unsigned int nYear)
{
m_Year = nYear;
}
void Date::SetMonth(unsigned int nMonth)
{
m_Month = nMonth;
}
void Date::SetDay(unsigned int nDay)
{
m_Day = nDay;
}
unsigned int Date::GetYear()
{
return m_Year;
}
unsigned int Date::GetMonth()
{
return m_Month;
}
unsigned int Date::GetDay()
{
return m_Day;
}
People::People(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay , unsigned int nId)
{
m_Number = nNumber;
m_Sex = nSex;
m_Birthday = new Date(nYear , nMonth , nDay);
m_Id = nId;
}
People::People()
{
}
People::~People()
{
}
People::People( const People &nObj )
{
m_Number = nObj.m_Number;
m_Sex = nObj.m_Sex;
m_Birthday = new Date(nObj.m_Birthday->GetYear() , nObj.m_Birthday->GetMonth() , nObj.m_Birthday->GetDay());
m_Id = nObj.m_Id;
}
void People::SetNumber(unsigned int nNumber)
{
m_Number = nNumber;
}
void People::SetSex(bool nSex)
{
m_Sex = nSex;
}
void People::SetBirthday(unsigned int nYear , unsigned int nMonth , unsigned int nDay)
{
m_Birthday->SetYear(nYear);
m_Birthday->SetMonth(nMonth);
m_Birthday->SetDay(nDay);
}
void People::SetId(unsigned int nId)
{
m_Id = nId;
}
void People::Display()
{
std::cout<< m_Number << " " << m_Sex << " " << m_Birthday->GetYear() << " " << m_Birthday->GetMonth() << " " << m_Birthday->GetDay() << " " << m_Id << std::endl;
}
Student::Student(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay , unsigned int nId)
{
People(nNumber , nSex , nYear , nMonth , nDay , nId );
test = 1.2;
}
//这里如果People没有显示的声明一个无参数的构造函数,则只能使用这种方式来构造Student
Student::Student(unsigned int nNumber , bool nSex , unsigned int nYear , unsigned int nMonth , unsigned int nDay , unsigned int nId) : People(nNumber , nSex , nYear , nMonth , nDay , nId )
{
}
Student::~Student()
{
}
void Student::DisplayVal()
{
std::cout << test << std::endl;
std::cout << People::test << std::endl;
}
void Student::ChangeTest( float nTest )
{
//基类和派生类的同名变量或函数
//在基类的作用域中,除虚函数外,都操作基类
//在派生类和外部的作用域中,都操作派生类,若想操作基类,则使用People::test来访问
test = nTest;
People::test = (int)nTest + 1;
}
int main()
{
People *sjn = new People( 1015 , 1 , 1994 , 8 , 29 , 1817 );
sjn->Display();
People *zjp = new People( *sjn );
zjp->SetId(1234);
zjp->SetBirthday( 1995 , 1 , 2 );
zjp->Display();
Student *stu = new Student( 1015 , 1 , 1994 , 8 , 29 , 1817 );
stu->ChangeTest(1.5);
stu->DisplayVal();
while(1);
return 0;
}
1.一个类可以有很多个构造函数
2.拷贝构造函数主要用于复制一个一模一样的对象
3.proteced关键字修饰的变量和函数派生类可以访问,外部不能访问
4.同名变量在基类和派生类以及外部实现时,如何区分使用?
在基类的作用域中,除了虚函数外,都操作基类
在派生类和外部的作用域中,都操作派生类,若想操作基类,则使用类名::变量名或方法名 来访问
5.继承的基类如果有参数,则需要在基类中显式的声明一个无参数的构造函数,这样才支持派生类的任意构造,否则,必须显式的使用派生类(参数...) : 基类(参数...)来构造派生类