美文网首页
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++ 继承 多态 泛型

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