058day(自增,自减运算符重载和继承与派生的基本概念)
2017-12-07 23:52
253 查看
172210704111-陈国佳总结《201712月7日》【连续058天】
标题:自增,自减运算符重载和继承与派生的基本概念;
内容:A.自增,自减运算符的重载:
由于自增,自减运算符有前置和后置之分,C++规定:
前置运算符作为一元运算符重载:
成员:T & operator++();
T & operator--();
全局:T1 &operator++(T2);
T2 &operator--(T2);
后置运算符作为二元运算符重载,多写一个没用的参数:
成员:T operator++(int);
T operatpor--(int);
全局:T1 operator++(T2,int);
T2 operator--(T2,int);
注:在没有后置重置而前置重置的情况下,vs中,obj++调用前置,dev中,obj++编译出错;
注:我们可以看出++a的返回值是a的引用,所以(++a)=1,a的值变成1,而a++=1出错;
class CDemo{
private :
int n;
public:
CDemo(int i=0):n(i){ }
CDemo & operator++();
CDemo operator++(int );
operator int (){ return n;}
friend CDemo & operator--(CDemo &);
friend CDemo operator--(CDemo &,int);
};
CDemo & CDemo::operator++()
{ //前置++
++n;
return*this;
}
CDemo CDemo::operator++(int )
{ //后置 ++
CDemo tmp(*this);
++n;
return tmp;
}
CDemo & operator--(CDemo & d)
{
d.n--;
return d;
}
CDemo operator--(CDemo &d,int)
{
CDemo tmp(d);
d.n--;
return tmp;
}
int main()
{
CDemo d(5);
cout<<(d++)<<","; //d.operator++(0)
cout<<d<<",";
cout<<(++d)<<","; //d.operator++()
cout<<d<<endl;
cout<<(d--)<<","; //operator--(d,0)
cout<<d<<",";
cout<<(--d)<<","; //operator--(d)
cout<<d<<endl;
return 0;
}
B.继承:一个新类B,与类A相似(B拥有A的全部特点),那么A可作为一个基类,B作为基类的派生类(子类)。
a)派生类是通过对基类进行修改(覆盖)和扩充得到的。
b)派生类一经定义后,可独立使用,不依赖于基类。
c)派生类拥有积累的全部函数和成员变量(无论private,protected,public),但派生类的成员函数不能访问基类中的private成员;
写法:
class 派生类名:public 基类名
{ };
派生类对象的内存空间等于基类对象的体积(存储位置在前)和派生类自己的成员变量体积(在后);
举例:
class CStudent{
private:
string name;
string id;
char gender;
int age;
public:
void PrintInfo();
void SetInfo(const string&name_,const string &id_,int age_,char gender_);
string GetName(){ return name;}
};
class CUndergraduateStudent:public CStudent{
private:
string department;
//多了系的名称
public:
void PrintInfo(){ //覆盖
CStudent::PrintInfo();
cout<<"Department:"<<department<<endl;
}
void SetInfo(const string&name_,const string &id_,int age_,char gender_,const string & department_)
{
CStudent::SetInfo(name_,id_,age_,gender_);
department =department_;
}
};
void CStudent::PrintInfo()
{
cout<<"Name:"<<name<<endl;
cout<<"ID:"<<id<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Gender:"<<gender<<endl;
}
void CStudent::SetInfo(const string&name_,const string &id_,int age_,char gender_)
{
name=name_;id=id_;age=age_;
gender=gender_;
}
int main()
{
CUndergraduateStudent s2;
s2.SetInfo("Potter","1188",19,'M',"Computer Scoence");
cout<<s2.GetName()<<" ";
s2.PrintInfo();
return 0;
}
明日计划:继承关系和复合关系;
标题:自增,自减运算符重载和继承与派生的基本概念;
内容:A.自增,自减运算符的重载:
由于自增,自减运算符有前置和后置之分,C++规定:
前置运算符作为一元运算符重载:
成员:T & operator++();
T & operator--();
全局:T1 &operator++(T2);
T2 &operator--(T2);
后置运算符作为二元运算符重载,多写一个没用的参数:
成员:T operator++(int);
T operatpor--(int);
全局:T1 operator++(T2,int);
T2 operator--(T2,int);
注:在没有后置重置而前置重置的情况下,vs中,obj++调用前置,dev中,obj++编译出错;
注:我们可以看出++a的返回值是a的引用,所以(++a)=1,a的值变成1,而a++=1出错;
class CDemo{
private :
int n;
public:
CDemo(int i=0):n(i){ }
CDemo & operator++();
CDemo operator++(int );
operator int (){ return n;}
friend CDemo & operator--(CDemo &);
friend CDemo operator--(CDemo &,int);
};
CDemo & CDemo::operator++()
{ //前置++
++n;
return*this;
}
CDemo CDemo::operator++(int )
{ //后置 ++
CDemo tmp(*this);
++n;
return tmp;
}
CDemo & operator--(CDemo & d)
{
d.n--;
return d;
}
CDemo operator--(CDemo &d,int)
{
CDemo tmp(d);
d.n--;
return tmp;
}
int main()
{
CDemo d(5);
cout<<(d++)<<","; //d.operator++(0)
cout<<d<<",";
cout<<(++d)<<","; //d.operator++()
cout<<d<<endl;
cout<<(d--)<<","; //operator--(d,0)
cout<<d<<",";
cout<<(--d)<<","; //operator--(d)
cout<<d<<endl;
return 0;
}
B.继承:一个新类B,与类A相似(B拥有A的全部特点),那么A可作为一个基类,B作为基类的派生类(子类)。
a)派生类是通过对基类进行修改(覆盖)和扩充得到的。
b)派生类一经定义后,可独立使用,不依赖于基类。
c)派生类拥有积累的全部函数和成员变量(无论private,protected,public),但派生类的成员函数不能访问基类中的private成员;
写法:
class 派生类名:public 基类名
{ };
派生类对象的内存空间等于基类对象的体积(存储位置在前)和派生类自己的成员变量体积(在后);
举例:
class CStudent{
private:
string name;
string id;
char gender;
int age;
public:
void PrintInfo();
void SetInfo(const string&name_,const string &id_,int age_,char gender_);
string GetName(){ return name;}
};
class CUndergraduateStudent:public CStudent{
private:
string department;
//多了系的名称
public:
void PrintInfo(){ //覆盖
CStudent::PrintInfo();
cout<<"Department:"<<department<<endl;
}
void SetInfo(const string&name_,const string &id_,int age_,char gender_,const string & department_)
{
CStudent::SetInfo(name_,id_,age_,gender_);
department =department_;
}
};
void CStudent::PrintInfo()
{
cout<<"Name:"<<name<<endl;
cout<<"ID:"<<id<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Gender:"<<gender<<endl;
}
void CStudent::SetInfo(const string&name_,const string &id_,int age_,char gender_)
{
name=name_;id=id_;age=age_;
gender=gender_;
}
int main()
{
CUndergraduateStudent s2;
s2.SetInfo("Potter","1188",19,'M',"Computer Scoence");
cout<<s2.GetName()<<" ";
s2.PrintInfo();
return 0;
}
明日计划:继承关系和复合关系;
相关文章推荐
- C++学习之路—继承与派生(一):基本概念与基类成员的访问属性
- c++类的继承与派生的基本概念和语法
- 1.1 继承和派生基本概念
- 基本概念:继承
- Java语言入门教程(十二):Java语言中继承之基本概念
- Java-面向对象(高级篇)--继承的基本概念
- 类的继承和派生概念
- 6.1 继承的基本概念
- 三十五、继承与派生:概念介绍与派生类的声明
- C++ - 继承与派生的概念
- c++基础10:继承和派生 虚函数的作用 多态性概念 纯虚函数和抽象类的概念
- (转)C++类继承基本概念解析
- O-c中类的继承与派生的概念
- C++学习6-面向对象编程基础(运算符重载、类的派生与继承、命名空间)
- O-c中类的继承与派生的概念
- 【C++学习笔记】继承与派生基础概念
- Time Date DateTime的继承与派生及运算符重载
- C++编程入门系列之三十五(继承与派生:概念介绍与派生类的声明)
- C++基本概念总结——公有继承和私有继承
- 从零开始学_JavaScript_系列(64)——class的继承(1)基本概念、继承构造函数和class