058day(自增,自减运算符重载和继承与派生的基本概念)

本文详细介绍了C++中自增、自减运算符的重载方法,包括前置与后置的区别及实现方式,并通过具体代码示例进行了说明。此外,还探讨了类的继承概念,包括派生类的创建、成员访问权限等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}




明日计划:继承关系和复合关系;



基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员用户两个主要模块。管理员负责视频、文件文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值