美文网首页
C++ 继承 多态 泛型

C++ 继承 多态 泛型

作者: 闪客飞飞 | 来源:发表于2021-03-17 20:03 被阅读0次

define _CRT_SECURE_NO_WARNINGS

include <stdlib.h>

include <iostream>

//标准命名空间(包含很多标准的定义)
//standard
using namespace std;
//命名空间类似于Java中包(归类)

//继承 主要解决代码的重用性

//class Human{
//public:
// void say(){
// cout << "说话" << endl;
// }
//protected:
// char* name;
// int age;
//};
//
//class Man : public Human{
//public:
// //工作
// void WorkIng(){
// cout << "工作" << endl;
// }
//private:
// //兄弟
// char* brother;
//};
//
//void work(Human &m){
// m.say();
//}
//void main(){
// Man m1;
// work(m1);
// m1.say();
//
// //子类类型初始化为父类类型的对象
// Human h12 = m1;
// //父类类型的引用指针指向子类
// Human h1 = &m1;
// h1->say();
// Human &h1w = m1;
// h1w.say();
//
// system("pause");
//}
//
//向父类构造方法传参
//人类
//class Human{
//public:
//Human(char
name, int age){
//this->name = name;
//this->age = age;
//}
//void say(){
//cout << "说话" << endl;
//}
//protected:
//char* name;
//int age;
//};
//
////男人
//class Man : public Human{
//public:
////给父类构造函数传参,同时给属性对象赋值
//Man(char *brother, char *s_name, int s_age, char h_name, int h_age) : Human(s_name, s_age), h(h_name,h_age){
//this->brother = brother;
//}
////泡妞
//void chasing(){
//cout << "泡妞" << endl;
//}
//private:
////兄弟
//char
brother;
//Human h;
//};
//
//void main(){
//Man m1("danny","jack",18,"jason",18);
//
//system("pause");
//}
//

//构造函数与析构函数调用的顺序

//class Human{
//public:
//Human(char* name, int age){
//this->name = name;
//this->age = age;
//cout << "Human 构造函数" << endl;
//}
//~Human(){
//cout << "Human 析构函数" << endl;
//}
//void say(){
//cout << "说话" << endl;
//}
//protected:
//char* name;
//int age;
//};
//
////男人
//class Man : public Human{
//public:
////给父类构造函数传参,同时给属性对象赋值
//Man(char *brother, char s_name,int s_age) : Human(s_name, s_age){
//this->brother = brother;
//cout << "Man 构造函数" << endl;
//}
//~Man(){
//cout << "Man 析构函数" << endl;
//}
////泡妞
//void chasing(){
//cout << "泡妞" << endl;
//}
//private:
////兄弟
//char
brother;
//};
//
//void func(){
////父类构造函数先调用
////子类的析构函数先调用
//Man m1("danny", "jack", 18);
//}
//
//void main(){
//func();
//
//system("pause");
//}
//

//子类对象调用父类的成员

//class Human{
//public:
//Human(char* name, int age){
//this->name = name;
//this->age = age;
//cout << "Human 构造函数" << endl;
//}
//~Human(){
//cout << "Human 析构函数" << endl;
//}
//void say(){
//cout << "说话" << endl;
//}
//public:
//char* name;
//int age;
//};
//
////男人
//class Man : public Human{
//public:
////给父类构造函数传参,同时给属性对象赋值
//Man(char *brother, char s_name, int s_age) : Human(s_name, s_age){
//this->brother = brother;
//cout << "Man 构造函数" << endl;
//}
//~Man(){
//cout << "Man 析构函数" << endl;
//}
////泡妞
//void chasing(){
//cout << "泡妞" << endl;
//}
//void say(){
//cout << "男人喜欢装逼" << endl;
//}
//private:
////兄弟
//char
brother;
//};

//多继承
/*
//人
class Person{
};
//公民
class Citizen{
};
//学生,既是人,又是公民
class Student : public Person, public Citizen{
};
*/

//继承的访问修饰
//基类中 继承方式 子类中
//public & public继承 = > public
//public & protected继承 = > protected
//public & private继承 = > private
//
//protected & public继承 = > protected
//protected & protected继承 = > protected
//protected & private继承 = > private
//
//private & public继承 = > 子类无权访问
//private & protected继承 = > 子类无权访问
//private & private继承 = > 子类无权访问

////继承的二意性
////virtual 虚继承 不同路径继承来的同名成员一份拷贝,解决不明确的问题。
//class A{
//public:
// char* name;
// void myprint(){
// cout << name << endl;
// }
//};
//class A1 : virtual public A{
//};
//class A2 : virtual public A{
//};
//class B :public A1,public A2{
//};
//void main(){
// B b;
// /b.A1::name="chengyue";
// b.A2::name = "eechengyue";
/
// b.name = "chhh";
// system("pause");
//}
//虚函数
//多态(程序的扩展性)
//静态多态:重载 动态多态:程序运行当中,觉得哪一个函数被调用(重写)

//发生动态多态的条件:
//1.继承
//2.父类的引用指向子类的对象
//3.重写父类的方法

//#include "Plane.h"
//#include "Jet.h"
//#include "Copter.h"
//void bizPlay(Plane & p){
// p.fly();
// p.land();
//}
//void main(){
// Plane p;
// bizPlay(p);
// Jet j;
// bizPlay(j);
// Copter c;
// bizPlay(c);
// system("pause");
//}
//虚函数 virtual 关键字产生抽象类
//纯虚函数(抽象类)
//1.当一个类具有一个纯虚函数 这个类就是抽象类。
//2.抽象类不能实例化对象
//3.子类继承抽象类必须要实现纯虚函数,如果没有子类也是抽象类

//形状
//class Shape{
//public:
// //纯虚函数
// virtual void sayArea() = 0;
// void print(){
// cout << "HI" << endl;
// };
//};
//class Circle :public Shape{
//public:
// Circle(int r){
// this->r = r;
// }
// void sayArea(){
// cout << "圆的面积" << 3.14rr << endl;
// }
//private:
// int r;
//};
//void main(){
//
// Circle c(14);
// c.sayArea();
// system("pause");
//}

//接口 (只是逻辑上的划分,语法上跟抽象类的写法没有区别)
//可以当做一个接口
//
//class Drawable{
// virtual void draw()=0;
//};

//函数模板 (泛型) 业务逻辑一样 数据类型不一样 根据实际类型 自动推导
void mySwap(int &a,int &b){
int temp = 0;
temp = a;
a = b;
b = temp;
}
template <typename T>
void mySwap(T &a, T &b){
int temp = 0;
temp = a;
a = b;
b = temp;
}
void main(){
int a = 10;
int b = 20;
char c = 'e';
char d = 'g';
cout << a << b << c << d << endl;
mySwap<int>(a,b);
mySwap(c, d);
cout << a << b << c << d << endl;
system("pause");
}

相关文章

  • C++ 继承 多态 泛型

    define _CRT_SECURE_NO_WARNINGS include include...

  • 1.0 STL基础

    1.1 C++实现软件的重用,体现在 (1)面向对象的思想:继承和多态,标准类库; (2)泛型程序设计的思想:模板...

  • 9.泛型(Thinking in java 学习九)

    多态算是一种泛化机制,但是拘泥于单继承体系,也会使程序受限太多。JavaSE5中提出了泛型的概念。 泛型概念:泛型...

  • java平台理解

    Java特性: 面向对象(封装,继承,多态) 平台无关性(JVM运行.class文件) 语言(泛型,Lambda)...

  • java面试题(杨晓峰)---第一讲谈谈你对java平台的理解

    本人总结: 面向对象(封装,继承,多态) 平台无关性(jvm运行,class文件) 语言(泛型,lambda) 类...

  • C++编译期多态和运行期多态

    C++多态有多种实现方式,在面对对象编程时,采用的是运行期多态,也称动态多态。在泛型编程中,多态基于模板的具现化与...

  • 多态,泛型,继承,封装,重载

    前言: 封装可以隐藏实现细节,使得代码模块化;继承可以扩展已存在的代码块(类);泛型是为了显现算法与类型脱离;它们...

  • Flow泛型(Generic Types)

    泛型(Generic Types) 使用泛型添加抽象(多态)类型。 泛型(有时被称为多态类型)是一种抽象类型的方法...

  • Java基础(一)

    Java要点1 JAVA基础 1.对抽象、继承、多态的理解 2.泛型的作用及使用场景 1.Java泛型是JDK1....

  • JVM学习之运行期优化知识点

    JAVA简介 基本语言特性(面向对象(封装,继承,多态),泛型,Lambda,反射) 平台无关性(JVM运行.cl...

网友评论

      本文标题:C++ 继承 多态 泛型

      本文链接:https://www.haomeiwen.com/subject/donecltx.html