1.纯虚函数

2.抽象类

3.内部类

4.运算符重载

5.类的函数重载

6.友元的函数重载

1.纯虚函数

==纯虚函数==

//有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数接口
#include <iostream>
#include <cstring>
using namespace std;
class Animal
{
public:
Animal(char *name);
void print_name();
virtual void print_color() = 0;
virtual ~Animal();
private:
char *m_Name; };
Animal::Animal(char *name)
{ m_Name = new char[strlen(name)+1];
strcpy(m_Name,name);
}
Animal::~Animal()
{
cout<<"Animal diluting !!!"<<endl;
if(m_Name)
{
delete[]m_Name;
}
}
void Animal::print_name()
{
cout<<"name: "<<m_Name<<endl;
}
class Cat:public Animal
{
public:
Cat(char *name,char *color);
virtual void print_color();
virtual ~Cat();
private:
char *m_Color;
};
Cat::Cat(char *name,char *color):Animal(name)
{
m_Color = new char[strlen(color)+1];
strcpy(m_Color,color);
}
void Cat:: print_color()
{
cout<<"Cat color :"<<m_Color<<endl;
}
Cat::~Cat()
{
cout<<"Cat diluting !!!"<<endl;
if(m_Color)
{
delete[]m_Color;
}
}
int main()
{
Animal *p;
p = new Cat("catmimi","yellow");
p->print_name();
p->print_color();
delete p;
return 0; }

2.抽象类

==抽象类==

//含有纯虚函数的基类,若派生类继承后仍有纯虚函数也为抽象类,直至无纯虚函数为止
#include<iostream>
#include<cstring>
using namespace std;
const double IP = 3.1415926;
class Shapes
{
public:
Shapes(int x,int y = 0);
virtual ~Shapes();
virtual void disp() = 0;
protected:
int m_X,m_Y;
};
Shapes::Shapes(int x,int y)
{
m_X = x;
m_Y = y;
}
Shapes::~Shapes()
{
cout<<"Shapes diluting !!!"<<endl;
}
class Squre:public Shapes
{
public:
Squre(int squre_x,int squre_y);
virtual void disp();
virtual ~Squre(); };
Squre::Squre(int squre_x,int squre_y):Shapes(squre_x,squre_y)
{ }
void Squre::disp()
{
cout<<"The squre area:"<<m_X*m_Y<<endl;
}
Squre::~Squre()
{
cout<<"Squre diluting !!!"<<endl;
}
class Circle:public Shapes
{
public:
Circle(int R);
virtual void disp();
virtual ~Circle();
};
Circle::Circle(int R):Shapes(R)
{ }
void Circle::disp()
{
cout<<"The circle area : "<<IP * m_X * m_X<<endl;
}
Circle::~Circle()
{
cout<<"Circle diluting !!!"<<endl;
}
int main()
{
Shapes *p[2];
p[0] = new Squre(5,10);
p[0] ->disp();
p[1] = new Circle(2);
p[1] ->disp();
for(int i;i<2;i++)
{
delete p[i];
}
return 0;
}

3.内部类

==内部类==

//在(基)类内部定义的类叫做内部类,(基)类相当于该类的外部类
#include <iostream>
using namespace std;
class Outer
{
public:
class Inner{
private:
int Inner_n;
public:
void set_outer_n(Outer &ref_outer){ref_outer.outer_n = 100;}
void set_inner_n(){Inner_n = 666;}
void show(Outer &ref_outer)
{
ref_outer.show();
cout<<"Inner: "<<Inner_n<<endl;
}
};
void show(){cout<<"Out: "<<outer_n<<endl;}
private:
int outer_n;
};
int main()
{
Outer outer_ojb;
Outer::Inner inner_ojb; inner_ojb.set_outer_n(outer_ojb);
inner_ojb.set_inner_n();
inner_ojb.show(outer_ojb);
return 0;
}

4.运算符重载

==运算符重载==
//如对象的相加,把特殊符号定义为函数易于表达
#include<iostream>
using namespace std;
class Q
{
private:
int x,y;
public:
Q(int x1=0,int y1=0):x(x1),y(y1){}
void show() const;
Q operator + (const Q&a)const ;###########重载+运算符
Q operator - (const Q&a)const ;
};
void Q::show() const
{
cout<<"(x,y)="<<"("<<x<<","<<y<<")"<<endl;
}
Q Q::operator + (const Q&a)const
{
return Q(x + a.x,y + a.y);
}
Q Q::operator - (const Q&a)const
{
return Q(x - a.x,y - a.y);
}
int main()
{
Q a1(7,2);
Q a2(5,1);
Q a;
Q b;
a = a1 + a2;
b = a1 - a2;
cout<<"a1:";
a1.show();
cout<<"a2:";
a2.show();
cout<<"a:";
a.show();
cout<<"b:";
b.show();
cout<<"a1+a2:";
a.show();
cout<<"a1-a2:";
b.show();
return 0; }

5.类的函数重载

==类的函数重载==

#include<iostream>
using namespace std;
class V
{
private:
int a,b;
public:
V(int a1 = 0,int b1 = 0):a(a1),b(b1){}
void show() const;
V operator ++();
V operator ++(int); };
void V::show() const
{
cout<<"(a,b)="<<"("<<a<<","<<b<<")"<<endl;
}
V V::operator ++() //++前置函数
{
++a;
++b;
return *this;
} V V::operator ++(int) //"k=i++":后缀++表示先k=i之后i自增
{
V a = *this;
++(*this); //调用前置函数
return a;
}
int main()
{
V m(1,1);
V n(0,0);
(m++).show();
(++n).show();
return 0;
}

6.友元的函数重载

==友元的函数重载==

//定义时不用指明函数位置,方便私有数据成员的调用
#include<iostream>
using namespace std;
class G
{
private:
int m,n;
public:
G(int m1 = 0,int n1 = 0):m(m1),n(n1){}
void show() const;
friend G operator + (const G&a1,const G&a2);
friend G operator - (const G&a1,const G&a2); };
void G::show() const
{
cout<<"(m,n)="<<"("<<m<<","<<n<<")"<<endl;
}
G operator + (const G&a1,const G&a2)
{
return G(a1.m +a2.m , a1.n +a2.n);
}
G operator - (const G&a1,const G&a2)
{
return G(a1.m - a2.m, a1.n - a2.n);
}
int main()
{
G j(2,2);
G k(1,1);
G a; j.show();
k.show();
a.show(); a = j + k;
a.show();
a = j - k;
a.show(); return 0; }

C++知识点案例 笔记-4的更多相关文章

  1. C++知识点案例 笔记-6

    1.三种友元函数 -非模板友元函数 -约束模板友元函数 -非约束模板友元函数 2.非类型参数 3.模板特化 1.三种友元函数 =====三种友元函数===== --1---非模板友元函数 #inclu ...

  2. C++知识点案例 笔记-5

    1.关系运算符重载 2.类型转换函数重载 3.转换构造函数 4.函数模板 5.显式实例化 6.类模板外定义模板函数 1.关系运算符重载 ==关系运算符重载== //直接(按分数)比较两个对象 #inc ...

  3. C++知识点案例 笔记-3

    1.基类指针等与派生类的兼容 2.构造函数 3.析构函数 4.虚基类 5.虚函数 6.虚析构函数 ==基类指针等与派生类的兼容== #include <iostream> #include ...

  4. C++知识点案例 笔记-2

    1.友元函数 2.友元类 3.继承(公有继承) 4.公有继承的访问权限 5.私有继承的访问权限 6.保护继承的访问权限(两次继承) ==友元函数== #include <iostream> ...

  5. C++知识点案例 笔记-1

    1.重载函数 2.内联函数 3.New.Delete 4.重载与.const形参 5.常数据成员 6.静态成员函数 ==重载函数== #include <iostream> using n ...

  6. Java后端高频知识点学习笔记1---Java基础

    Java后端高频知识点学习笔记1---Java基础 参考地址:牛_客_网 https://www.nowcoder.com/discuss/819297 1.重载和重写的区别 重载:同一类中多个同名方 ...

  7. [置顶] 单片机C语言易错知识点经验笔记

    今天写这一篇文章并不是因为已经想好了一篇文章才写下来,而是我要将这一篇文章作为一个长期的笔记来写,我会一直更新.在进行单片机开发时,经常都会出现一些很不起眼的问题,这些问题其实都是很基础的c语言知识点 ...

  8. 面试总结:鹅厂Linux后台开发面试笔试C++知识点参考笔记

    文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 文章是由自己笔试面试腾讯的笔记整理而来,整理的时候又回顾了一遍,中间工 ...

  9. php 知识点 --个人笔记

    ##2015-09-06 为防止用户看到错误信息,而出现的不友好界面.故一般性会在php.ini里设置:display_errors = Off;不过在开发的时候,我们有时候需要打开错误信息.这时候, ...

随机推荐

  1. 基于scrapy框架的爬虫基本步骤

    本文以爬取网站 代码的边城 为例 1.安装scrapy框架 详细教程可以查看本站文章 点击跳转 2.新建scrapy项目 生成一个爬虫文件.在指定的目录打开cmd.exe文件,输入代码 scrapy ...

  2. Redis解读(2):Redis的Java客户端

    Redis的Java客户端 Redis不仅使用命令客户端来操作,而且可以使用程序客户端操作,其实配置和实现起来也非常容易. 现在基本上主流的语言都有客户端支持,比如Java.C.C#.C++.php. ...

  3. 呵呵,Semaphore,就这?

    这是并发线程工具类的第二篇文章,在第一篇中,我们分析过 CountDownLatch 的相关内容,你可以参考 一文搞懂 CountDownLatch 用法和源码! 那么本篇文章我们继续来和你聊聊并发工 ...

  4. 算法:第一节课Master定理

    1.ctex:要求用Tex编辑器进行作业的书写 2.与东大本科有差距,还需要多点努力才行. 3. 4.考试不考概念 5. 6.时间复杂度和空间复杂度 7.算法好坏的评价标准 8.基本运算 9.时间复杂 ...

  5. 【DB宝49】Oracle如何设置DB、监听和EM开机启动

    目录 一.Windows系统 二.Linux系统 方法1:配置/etc/rc.d/rc.local文件(推荐) 方法2:配置service服务 三.Oracle 18c版本 四.总结 一.Window ...

  6. k8s helm 安装etcd

    待续 helm install etcd bitnami/etcd \ --set statefulset.replicaCount=3 \ --set persistence.enabled=tru ...

  7. 脱壳——修复加密IAT

    脱壳--修复加密IAT 对两个练手程序进行脱壳修复加密IAT(其实是一个程序,只是用了几种不同的加壳方式) 第一个程序 Aspack.exe 下载链接:https://download.csdn.ne ...

  8. php图片合成【png图片】

    php 图片合成[png图片] 示例代码 <?php header("Content-type:text/html;charset=utf-8"); error_report ...

  9. 【工具类】获取Http请求IP的工具类

    public class IpAddressUtil { public static String getIpAddr(HttpServletRequest request){ String ipAd ...

  10. UC-Android逆向工程师 面试题1的分析

    1.简介 这个题目是一位吾爱破解的坛友在面试UC的Android逆向工程事时,遇到的题目.此题不难,与阿里移动去年移动安全比赛的题目差不多,题目的验证方式也是查表对比,并且这个表的数据是放在文件中的. ...