C++知识点案例 笔记-4
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的更多相关文章
- C++知识点案例 笔记-6
1.三种友元函数 -非模板友元函数 -约束模板友元函数 -非约束模板友元函数 2.非类型参数 3.模板特化 1.三种友元函数 =====三种友元函数===== --1---非模板友元函数 #inclu ...
- C++知识点案例 笔记-5
1.关系运算符重载 2.类型转换函数重载 3.转换构造函数 4.函数模板 5.显式实例化 6.类模板外定义模板函数 1.关系运算符重载 ==关系运算符重载== //直接(按分数)比较两个对象 #inc ...
- C++知识点案例 笔记-3
1.基类指针等与派生类的兼容 2.构造函数 3.析构函数 4.虚基类 5.虚函数 6.虚析构函数 ==基类指针等与派生类的兼容== #include <iostream> #include ...
- C++知识点案例 笔记-2
1.友元函数 2.友元类 3.继承(公有继承) 4.公有继承的访问权限 5.私有继承的访问权限 6.保护继承的访问权限(两次继承) ==友元函数== #include <iostream> ...
- C++知识点案例 笔记-1
1.重载函数 2.内联函数 3.New.Delete 4.重载与.const形参 5.常数据成员 6.静态成员函数 ==重载函数== #include <iostream> using n ...
- Java后端高频知识点学习笔记1---Java基础
Java后端高频知识点学习笔记1---Java基础 参考地址:牛_客_网 https://www.nowcoder.com/discuss/819297 1.重载和重写的区别 重载:同一类中多个同名方 ...
- [置顶] 单片机C语言易错知识点经验笔记
今天写这一篇文章并不是因为已经想好了一篇文章才写下来,而是我要将这一篇文章作为一个长期的笔记来写,我会一直更新.在进行单片机开发时,经常都会出现一些很不起眼的问题,这些问题其实都是很基础的c语言知识点 ...
- 面试总结:鹅厂Linux后台开发面试笔试C++知识点参考笔记
文章每周持续更新,各位的「三连」是对我最大的肯定.可以微信搜索公众号「 后端技术学堂 」第一时间阅读(一般比博客早更新一到两篇) 文章是由自己笔试面试腾讯的笔记整理而来,整理的时候又回顾了一遍,中间工 ...
- php 知识点 --个人笔记
##2015-09-06 为防止用户看到错误信息,而出现的不友好界面.故一般性会在php.ini里设置:display_errors = Off;不过在开发的时候,我们有时候需要打开错误信息.这时候, ...
随机推荐
- JVM学习笔记(一):JVM初探
1 来源 来源:<Java虚拟机 JVM故障诊断与性能优化>--葛一鸣 章节:第一章 本文是第一章的一些笔记整理. 2 Java里程碑 2.1 Java起源 1990年Sun公司决定开发一 ...
- JavaFX获取屏幕尺寸
1 awt Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); double width = screenSize. ...
- Day09_42_Set集合_HashSet
集合之HashSet * HashSet - HashSet无序不可重复,HashSet底层实际上是一个HashMap,HashMap底层采用了Hash表数据结构. - 哈希表又叫做散列表,哈希表底层 ...
- Day11_49_HashTable
HashTable * HashTable是较早期的使用Hash算法的一种容器结构,现在基本已被淘汰,单线程多使用HashMap,多线程使用ConcurrentHashMap. * HashTable ...
- JVM小册(1)------jstat和Parallel GC日志
JVM小册(1)------jstat和Parallel GC日志 一. 背景 在生产环境中,有时候会遇到OOM的情况,抛开Arthas 等比较成熟的工具以外,我们可以使用java 提供的jatat和 ...
- 963. Minimum Area Rectangle II
Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...
- hdu4415 不错的想法题
题意: 一个人他有一定的血,有一些怪物,他去杀怪物,有的怪物杀死他后还可以在不费自己血的情况下任意杀死一些怪物,问你他最多杀死多少怪物,在最多杀怪前提下最好用多少血,(大体题意是这样). 思路: 首先 ...
- hdu1542 线段树扫描线求矩形面积的并
题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...
- 半自动二进制协议模糊工具 Peach 使用
链接:https://bbs.ichunqiu.com/thread-54487-1-1.html
- layui中的多图上传
效果展示: 1.html部分: 注:<input> 作为隐藏域,用于保存多图上传的资源数组,方便后期进行 form 表单的提交 <input type="hidden&qu ...