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. 小心,别被eureka坑了

    Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的.SpringCloud将它集成在其子项 ...

  2. OO_Unit1_Summary

    经历了十分充实(痛不欲生)的三周不一样的码代码的生活,让我对通宵oo有了新的认识.往届学长学姐诚不欺我 第一次作业 需求分析 第一次需求非常简单(相比较后两次作业而言),仅为简单多项式求导,而且仅包含 ...

  3. SpringCloud LoadBalancer灰度策略实现

    如何使用 Spring Cloud 2020 中重磅推荐的负载均衡器 Spring Cloud LoadBalancer (下文简称 SCL),如何扩展负载均衡策略? 你将从本文中获取到答案 快速上手 ...

  4. Linux 递归修改后缀名

    1 修改命令 需要用到: find awk xargs 递归修改命令如下: find . -name '*.XXX' | awk -F "." '{print $2}' | xar ...

  5. Socket 多任务(多进程/线程、I/O多路复用、事件驱动开发框架)

    0. 概述 1. 循环版实现多连接 2. threading.Thread 多线程 3. SockerServer 实现多任务 3.1 ForkingMixIn - 多进程(限 linux) 3.2 ...

  6. 数据结构(2):单链表学习使用java实现

    单链表是单向链表,它指向一个位置: 单链表常用使用场景:根据序号排序,然后存储起来. 代码Demo: package com.Exercise.DataStructure_Algorithm.Sing ...

  7. 473. Matchsticks to Square

    Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match ...

  8. html书签展示(带搜索)

    源代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...

  9. try catch 用法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. PAT 乙级 -- 1008 -- 数组元素循环右移问题

    题目简述 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0 A1--AN-1)变换为(AN-M -- AN ...