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. Spring Boot 2.3.0 新特性Redis 拓扑动态感应

    本文为原创文章.欢迎任何形式的转载,但请务必注明出处 冷冷https://lltx.github.io. Spring Boot 2.3 新特性优雅停机详解 Spring Boot 2.3 新特性分层 ...

  2. 9. VUE 常用正则表达式

    1. 判断输入是否是数字 var numReg = /^[0-9]+$/ var numRe = new RegExp(numReg) if (!numRe.test(number)) { this. ...

  3. 2. IntelliJ Idea 常用快捷键列表

    Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关闭文件 Ctrl+[ OR ],可以跑到大括号的开头与结尾 Ctrl+F12,可以显示当前文件的结构 ...

  4. Mysql之读写分离架构-Atlas

    Atlas介绍 1.png ​ Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目. 它是在mysql-proxy 0.8.2版本的基础上, ...

  5. JAVAEE_Servlet_20_登录注册功能

    实现登录注册功能 注册功能 import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import j ...

  6. Day11_54_泛型(Generic)

    泛型 * Java泛型设计原则:只要在编译时期没有出现警告,那么运行时期就不会出现ClassCastException异常. * 泛型:把类型明确的工作推迟到创建对象或调用方法的时候才去明确的特殊的类 ...

  7. 分页系列之一:SQL Server 分页存储过程

    以下为最基本的代码结构,SQL Server 2012 开始支持 CREATE PROCEDURE procXXX @Page int, --当前页码,从1开始 @PageSize int --每页记 ...

  8. 如何把 Caffeine Cache 用得如丝般顺滑?

    一.关于 Caffeine Cache 在推荐服务中,虽然允许少量请求因计算超时等原因返回默认列表.但从运营指标来说,越高的"完算率"意味着越完整的算法效果呈现,也意味着越高的商业 ...

  9. Qt获取一张图片的平均色(主色调)

    这两天在一个小工具中想做一个图标的发光效果,用固定颜色做出来效果很丑,于是想到此方法,得到图标的主色调后,将颜色调亮,并设置为阴影颜色,从而达到类似发光的效果. 本文章主要在于得到一张图片的平均色,并 ...

  10. 1. APP移动端性能测试基础知识入门

    本博客要点 生命周期 堆和栈 垃圾回收 adb命令 Activity的生命周期