1.友元函数

2.友元类

3.继承(公有继承)

4.公有继承的访问权限

5.私有继承的访问权限

6.保护继承的访问权限(两次继承)

==友元函数==

#include <iostream>
using namespace std;
class love
{
public:
love(int you,int me);
friend int baby(love& );//################
private:
int Me;
int You;
};
love::love(int you,int me)
{
You = you;
Me = me;
}
int baby(love& refnum)
{
return refnum.You + refnum.Me ;//###########
}
int main()
{
love aini(500,21);
cout<<"我想对你说:"<<baby(aini)<<endl;
return 0;
}
==友元类==
#include <iostream>
using namespace std;
class Date; //声明Date类
class Time //定义Time类,描述时分秒
{
public:
Time(int con_hour, int con_minute, int con_second) //定义构造函数
{
m_nHour = con_hour;
m_nMinute = con_minute;
m_nSecond = con_second; }
friend class Date; //声明Date为Time的友元类
private:
int m_nHour, m_nMinute, m_nSecond;
};
class Date //定义Date类
{
public:
Date(int con_year, int con_month, int con_day)//定义构造函数
{
m_nYear = con_year;
m_nMonth = con_month;
m_nDay = con_day;
}
void display_date_time(Time &ref_time); //声明display_date_time()函数
private:
int m_nYear, m_nMonth, m_nDay;
};
/*
* 定义display_date_time()函数,显示年月日、时分秒信息。由于Date为Time类的友元类,
* 则Date中的函数为Time类的友元函数,可以访问Time类的私有成员,
* 这里直接访问了私有成员:m_nHour、m_nMinute、m_nSecond
*/
void Date::display_date_time(Time &ref_time)
{
cout << m_nYear << "-" << m_nMonth << "-" << m_nDay << " "
<< ref_time.m_nHour << ":" << ref_time.m_nMinute//################
<< ":" << ref_time.m_nSecond << endl; //###############
}
int main()
{
Time time(17, 30, 20); //定义Time对象
Date date(2017, 2, 23); //定义Date对象
date.display_date_time(time); //显示年月日、时分秒信息
return 0;
}

==继承(公有继承)==
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
void speak()
{
cout<<"animal language."<<endl; }
};
class Dog :public Animal //继承Animal的属性
{
public:
Dog (string con_name):m_Name(con_name){}
void print_name()
{
cout<<"con_name:"<<m_Name<<endl;
}
private:
string m_Name; };
int main()
{
Dog dog("dogoo");
dog.speak(); //调用其继承的Animal属性里的speak函数
dog.print_name();
return 0;
}
==公有继承的访问权限==
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
void set_weight(int weight){m_Weight = weight;}
int get_weight(){return m_Weight;}
void set_age(int age){m_Age = age;}
protected:
int m_Age; //protected成员可以被派生类访问
private:
int m_Weight; //私有成员被派生类不可访问
};
class Dog:public Animal
{
public:
Dog (string con_name):m_Name(con_name){}
void print_age(){ cout<<m_Name<<",age="<<m_Age<<endl; } //这里直接访问了//#################
private:
string m_Name;
};
int main()
{
Dog dog("dogoo");
dog.set_weight(6);
dog.set_age(8);
dog.print_age();
cout<<"weight ="<<dog.get_weight()<<endl; //这里通过继承基类成员函数输出weight值,不可直接访问
return 0; }
==私有继承的访问权限==
//把从基类继承的protected、public成员当做私有成员,主函数不能直接访问基类的成员
#include <iostream>
#include <string>
using namespace std;
class Animal
{
public:
void set_weight(int weight){m_Weight = weight;}
int get_weight(){return m_Weight;}
void set_age(int age){ m_Age = age; }
protected:
int m_Age;
private:
int m_Weight;
};
class Dog:private Animal
{
public:
Dog(string con_name):m_Name(con_name){}
void set_print_weight()
{
set_weight(6); //################
cout<<m_Name<<",weight = "<<get_weight()<<endl;
}
void set_print_age()
{
set_age(8); //#################
cout<<m_Name<<",age = "<<m_Age<<endl;
}
private:
string m_Name;
};
int main()
{
Dog dog("dogoo");
dog.set_print_weight(); //通过派生类的访问来访问基类成员
dog.set_print_age();
return 0;
}
==保护继承的访问权限(多重继承:类Animal——>次类Dog——>再次类Dog_plus)==
//把从基类继承的protected、public成员当做保护成员
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class Animal
{
public:
void set_weight(int weight){m_Weight = weight;}
int get_weight(){return m_Weight;}
void set_age(int age){m_Age = age;}
protected:
int m_Age;
private:
int m_Weight;
};
class Dog:protected Animal //#################
{ public:
Dog(string con_name):m_Name(con_name){}
void set_print_weight()
{
set_weight(8);
cout<<m_Name<<",weight ="<<get_weight()<<endl;
}
private:
string m_Name;
};
class plus_Dog:protected Dog //###################
{
public:
plus_Dog():Dog("dogoo"){ }
void plus_set_print_age()
{
set_age(6);
cout<<"The plus_dog age = "<<m_Age<<endl;
} };
int main()
{
plus_Dog plus_dog;
plus_dog.plus_set_print_age();
system("pause");
return 0;
}

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

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

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

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

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

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

    1.纯虚函数 2.抽象类 3.内部类 4.运算符重载 5.类的函数重载 6.友元的函数重载 1.纯虚函数 ==纯虚函数== //有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数 ...

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

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

  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. Dynamics CRM邮箱配置

    Dynamics CRM对邮箱有很好的支持,开通邮箱后方便用户通过邮件进行Dynamics CRM的业务处理,同时也可以作为一直消息流提醒的手段应用于审批.通知等场景,可以做一些更深入的功能拓展. 本 ...

  2. Recoil Input 光标位置被重置到末尾的问题

    考察如下代码,页面中有个输入框,通过 Recoil Atom 来存储输入的值. App.tsx function NameInput() { const [name, setName] = useRe ...

  3. JavaWeb 补充(Json)

    HTML DOM alert() 方法 定义和用法 alert() 方法用于显示带有一条指定消息和一个 OK 按钮的警告框. 参数 描述 message 要在 window 上弹出的对话框中显示的纯文 ...

  4. Spring Boot 接口幂等插件使用

    幂等概述 幂等性原本是数学上的概念,即使公式:f(x)=f(f(x)) 能够成立的数学性质.用在编程领域,则意为对同一个系统,使用同样的条件,一次请求和重复的多次请求对系统资源的影响是一致的. 幂等性 ...

  5. 1151 LCA in a Binary Tree (30point(s))

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  6. 753. Cracking the Safe

    There is a box protected by a password. The password is n digits, where each letter can be one of th ...

  7. 【Idea】实用的快捷键清单

    1.Ctrl + Shift +i:快速查看某个类/方法 2.Ctrl +:(Ace Jump插件启动) 3.alt+F1:快速查看某个类/方法 所在的包 4.Ctrl +w :选中某个单词 5.Ct ...

  8. ART模式下基于dex2oat脱壳的原理分析

    本文博客地址:http://blog.csdn.net/qq1084283172/article/details/78513483 一般情况下,Android Dex文件在加载到内存之前需要先对dex ...

  9. 基于三层交换机的VRRP技术--MSTP、VRRP的综合运用

    MSTP (多生成树) 每个VLAN或者几个VLAN拥有一颗生成树,基于实例的生成树.instance 1.instance 2 每个实例拥有一颗生成树.MSTP可以实现多VLAN 的负载分担,可以实 ...

  10. Linux中的网络配置

    目录 网卡的配置 NetworkManager的使用 Team网卡绑定 Centos6.5.Redhat7.Kali网卡配置的不同 Kali桥接模式配置静态ip 网卡的配置 网卡命名的不同: Rhel ...