IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ 面向对象 -> 正文阅读

[C++知识库]C++ 面向对象

#include <iostream>

class Course {
private:
    std::string name;
public:
    // 方式1 推荐
     Course(std::string name) :name(name) {}
     
    // 方式2
//    Course(std::string name) {
//        this->name = name;
//    }
};

class People {
private:
    std::string name;
    int age = 0;
public:
    People(std::string name, int age) :name(name), age(age) {}
};

class Student : public People{
private:
    Course course;
public:
    // 方式1 给对象course初始化
//    Student(std::string name, int age, Course course1, std::string courseName) :People(name, age), course(course1) {}

    // 方式2 给对象course初始化  构造函数
    Student(std::string name, int age, Course course1, std::string courseName) :People(name, age), course(courseName) {}
};

int main() {
    Course course("C++");
    Student student("zhangSan", 23, course, "c++++++");

    return 0;
}

构造函数

构造函数 方式1

#include <iostream>

class People {
public:  // 构造函数
    // 无参构造函数
    People() {
        std::cout << "空参构造函数" << std::endl;
    }

    // 一个参数 的 构造函数
//    People(char * name) {
//        this->name = name;
//    }
    // 等价于
//    People(char * name) :name(name) {}

    // 一个参数构造函数   先去调用两个参数的构造函数,再调用1个参数的构造函数
    People(char * name) :People(name, 0){
        std::cout << "1个参数沟通函数." << name << std::endl;
    }

    // 两个参数 的 构造函数
    People(char * name, int age) :name(name), age(age) {
        std::cout << "2个参数构造函数." << name << ", " << age << std::endl;
    }

private:  // 私有属性
    char * name;
    int age;

public:  // get  set
    char * getName(){
        return this->name;
    }
    int getAge() {
        return this->age;
    }
    void setName(char * name) {
        this->name = name;
    }
    void setAge(int age){
        this->age = age;
    }
};

int main() {
    // 栈区
    // 方式1
    People people;  // 空参构造函数
    people.setName("zhangSan");
    people.setAge(23);
    std::cout<< people.getName() << ", " << people.getAge() << std::endl;
    std::cout<< " ------------------ " << std::endl;

    // 方式2
    People people2("liSi", 24);  // 2个参数构造函数
    std::cout<< people2.getName() << ", " << people2.getAge() << std::endl;
    std::cout<< " ------------------ " << std::endl;

    // 方式3
    People people3("wangWu");  // 1个参数的构造函数
    std::cout<< " ------------------------------------ " << std::endl;


    // 堆区
    // 方式4
    People * people4 = new People("liuLiu", 26);
    std::cout<< people4->getName() << ", " << people4->getAge() << std::endl;
    delete people4;

    return 0;
}

// 结果打印:
//空参构造函数
//zhangSan, 23
// ------------------
//2个参数构造函数.liSi, 24
//liSi, 24
// ------------------
//2个参数构造函数.wangWu, 0
//1个参数沟通函数.wangWu
// ------------------------------------
//2个参数构造函数.liuLiu, 26
//liuLiu, 26

构造函数 方式2

CMakeLists.txt

在这里插入图片描述

People.h

#include <iostream>

class People {
public:  // 构造函数
    // 无参构造函数
    People();

    // 一个参数构造函数
    People(char * name);

    // 两个参数
    People(char * name, int age);

private:  // 私有属性
    char * name;
    int age;

public:  // get  set
    char * getName();
    int getAge();
    void setName(char * name);
    void setAge(int age);
};

People.cpp

#include "People.h"

People::People() {
    std::cout << "empty parameter" << std::endl;
}

People::People(char *name) :People(name, 0){
    this->name = name;
    std::cout << "one parameter." << name << std::endl;
}

People::People(char *name, int age) {
    this->name = name;
    this->age = age;
    std::cout << "two parameter." << name << ", " << age << std::endl;
}

char * People::getName(){
    return this->name;
}

int People::getAge() {
    return this->age;
}

void People::setName(char * name) {
    this->name = name;
}

void People::setAge(int age){
    this->age = age;
}

main.cpp

#include "People.h"

int main() {
    // 栈区
    // 方式1
    People people;  // 空参构造函数
    people.setName("zhangSan");
    people.setAge(23);
    std::cout<< people.getName() << ", " << people.getAge() << std::endl;
    std::cout<< " ------------------ " << std::endl;

    // 方式2
    People people2("liSi", 24);  // 2个参数构造函数
    std::cout<< people2.getName() << ", " << people2.getAge() << std::endl;
    std::cout<< " ------------------ " << std::endl;

    // 方式3
    People people3("wangWu");  // 1个参数的构造函数
    std::cout<< " ------------------------------------ " << std::endl;


    // 堆区
    // 方式4
    People * people4 = new People("liuLiu", 26);
    std::cout<< people4->getName() << ", " << people4->getAge() << std::endl;
    delete people4;
    people4 = NULL;

    return 0;
}
//empty parameter
//zhangSan, 23
// ------------------
//two parameter.liSi, 24
//liSi, 24
// ------------------
//two parameter.wangWu, 0
//one parameter.wangWu
// ------------------------------------
//two parameter.liuLiu, 26
//liuLiu, 26

析构函数

CMakeLists.txt

在这里插入图片描述

People.h

#include <iostream>

class People {
public:  // 构造函数
    // 无参构造函数
    People();

    // 一个参数构造函数
    People(char * name);

    // 析构函数
    ~People();

private:  // 私有属性
    char * name;

public:  // get  set
    char * getName();
    void setName(char * name);
};

People.cpp

#include "People.h"

People::People() {
    std::cout << "empty parameter" << std::endl;
}

People::People(char *name) {
    this->name = name;
    std::cout << "one parameter." << name << std::endl;
}
// delete people的时候,会调用析构函数
People::~People() {
    std::cout << "~People." << std::endl;
}

char * People::getName(){
    return this->name;
}

void People::setName(char * name) {
    this->name = name;
}

main.cpp

#include "People.h"

int main() {
    People * people = new People("liuLiu");
    std::cout<< people->getName() << std::endl;
    delete people;
    people = NULL;

    return 0;
}

//one parameter.liuLiu
//liuLiu
//~People.

构造函数和析构函数调用顺序

#include <iostream>

class Person {
public:
    char * name;
    Person(char * name) : name(name) {
        std::cout << "Person构造函数" << std::endl;
    }
    ~Person()  {
        std::cout << "Person析构函数" << std::endl;
    }
};

class Student : public Person {
public:
    char * name;
    Student(char * name) : Person(name) {
        std::cout << "Student构造函数" << std::endl;
    }
    ~Student()  {
        std::cout << "Student析构函数" << std::endl;
    }
};

int main() {
    Student student("Derry");
    
    return 0;
}
// 打印记录:
// Person构造函数
// Student构造函数
// Student析构函数
// Person析构函数

友元函数

友元函数1

#include <iostream>

class People {
private:
    int age = NULL;

public:
    People(int age) :age(age){}
    int getAge() {
        return this->age;
    }
    // 定义友元函数  声明
    friend void updateAge(People * people, int age);
};

// 实现
void updateAge(People * people, int age) {
    // 默认不能修改私有的age,通过友元函数可以修改
    people->age = age;
}

int main() {
    People people1 = People(23);
    updateAge(&people1, 88);

    std::cout << "people1.getAge(): " << people1.getAge() << std::endl;

    return 0;
}

友元函数2

CMakeLists.txt

cmake_minimum_required(VERSION 3.17)
project(mycpptest)

set(CMAKE_CXX_STANDARD 14)

add_executable(mycpptest main.cpp People.cpp People.h)

People.h

#include <iostream>

class People {
private:
    int age = NULL;

public:
    People(int age);
    int getAge();
    // 友元函数  声明
    friend void updateAge(People * people, int age);
};

People.cpp

#include "People.h"

People::People(int age) {
    this->age = age;
}

int People::getAge() {
    return this->age;
}

// 友元函数实现 不需要 对象::,只需要保证函数名和参数即可
void updateAge(People * people, int age) {
    people->age = age;
}

main.cpp

#include "People.h"

int main() {
    People people1 = People(23);
    updateAge(&people1, 88);

    std::cout << "people1.getAge(): " << people1.getAge() << std::endl;

    return 0;
}

友元类

#include <iostream>

class People {
private:
    int age = NULL;
    friend class Class; // 友元类
};

class Class {
public:
    People people;
    void changeAge(int age) {
        people.age = age;
    }
    int getAge() {
        return people.age;
    }
};

int main() {
    Class peopleClass;
    peopleClass.changeAge(22);

    std::cout << peopleClass.getAge() << std::endl;

    return 0;
}

运算符重载

类外运算符重载

#include <iostream>

class People {
private:
    int number1, number2;
public:
    People(int number1, int number2) :number1(number1), number2(number2) {}
    void setNumber1(int number1) {
        this->number1 = number1;
    }
    void setNumber2(int number2) {
        this->number2 = number2;
    }
    int getNumber1(){
        return this->number1;
    }
    int getNumber2(){
        return this->number2;
    }
};

People operator + (People people1, People people2) {
    int number1 = people1.getNumber1() + people2.getNumber1();
    int number2 = people1.getNumber2() + people2.getNumber2();
    People peo(number1, number2);
    return peo;
}

int main() {
    People people1(10, 20);
    People people2(30, 40);

    People people = people1 + people2;

    std::cout << people.getNumber1() << ", " << people.getNumber2()<< std::endl;

    return 0;
}

类内运算重载

#include <iostream>

class People {
private:
    int number1, number2;
public:
    People(int number1, int number2) :number1(number1), number2(number2) {}
    void setNumber1(int number1) {
        this->number1 = number1;
    }
    void setNumber2(int number2) {
        this->number2 = number2;
    }
    int getNumber1(){
        return this->number1;
    }
    int getNumber2(){
        return this->number2;
    }
    // 方式1
//    People operator + (People people){
//        int num1 = this->number1 + people.getNumber1();
//        int num2 = this->number2 + people.getNumber2();
//        return People(num1 ,num2);
//    }
    // 方式2
    People operator + (const People & people){
        int num1 = this->number1 + people.number1;
        int num2 = this->number2 + people.number2;
        return People(num1 ,num2);
    }

    // ++a
    void operator ++ () {
        this->number1 = this->number1 + 1;
        this->number2 = this->number2 + 1;
    }

    // a++
    void operator ++ (int) {
        this->number1 = this->number1 + 1;
        this->number2 = this->number2 + 1;
    }


};

int main() {
    People people1(10, 20);
    People people2(30, 40);
    People people = people1 + people2;
    std::cout << people.getNumber1() << ", " << people.getNumber2()<< std::endl;

    People people3(100, 200);
    people3++;
    ++people3;
    std::cout << people3.getNumber1() << ", " << people3.getNumber2()<< std::endl;

    return 0;
}

继承

#include <iostream>

class People {
private:
    char * name;
    int age = NULL;
public:
    People(char * name, int age) :name(name), age(age) {
        std::cout << "Person 构造函数" << std::endl;
    }
    void print() {
        std::cout << this->name << ", " << this->age << std::endl;
    }
};

// 公开继承
class Student : public People{
private:
    char * address;
public:
    Student(char * name, int age, char * address) :People(name, age) , address(address){
        std::cout << "Student 构造函数" << std::endl;
    }

    void test() {
        print();
    }
};

int main() {
    Student student("zhangSan", 21, "xxx");
    student.test();

    return 0;
}

多继承1

#include <iostream>

class People1 {
public:
    void show(){
        std::cout << "People1 show" << std::endl;
    }
    void play(){
        std::cout << "People1 play" << std::endl;
    }
};

class People2 {
public:
    void study(){
        std::cout << "People2 study" << std::endl;
    }
    void play(){
        std::cout << "People2 play" << std::endl;
    }
};

// 公开继承
class Student : public People1, public People2{
public:
    void study(){
        std::cout << "Student study" << std::endl;
    }
};

int main() {
    Student student;
    student.study();

    // 会有歧义
//    student.play();

    // 避免歧义
    student.People1::play();
    student.People2::play();
    
    return 0;
}

多继承2

#include <iostream>

class Object {
public:
    int number;
};

class People1 : public Object{

};

class People2 : public Object{

};

// 公开继承
class Student : public People1, public People2{
public:
    int number;
};

int main() {
    Student student;
    // 有歧义
//    student.number;

    // 避免歧义
    student.People1::number;
    student.People2::number;
    
    // 避免歧义,子类覆盖掉即可
    student.number;

    return 0;
}

虚基类

#include <iostream>

class Object {
public:
    int number;
};

class People1 : virtual public Object{

};

class People2 : virtual public Object{

};

// 公开继承
class Student : public People1, public People2{

};

int main() {
    Object object;
    People1 people1;
    People2 people2;
    Student student;

    object.number = 1;
    people1.number = 2;
    people2.number = 3;
    student.number = 4;

    std::cout << object.number << std::endl;  // 1
    std::cout << people1.number << std::endl; // 2
    std::cout << people2.number << std::endl; // 3
    std::cout << student.number << std::endl; // 4

    return 0;
}

多态

动态多态

动态多态:程序在运行期间才能确定调用哪个类的函数
C++ 默认关闭多态,加上virtual即可

#include <iostream>

class BaseActivity {
public:
    // C++ 默认关闭多态,加上virtual即可
    virtual void onStart() {
        std::cout << "BaseActivity onStart" << std::endl;
    }
};

class HomeActivity : public BaseActivity{
public:
    void onStart() {
        std::cout << "HomeActivity onStart" << std::endl;
    }
};

class LoginActivity : public BaseActivity{
public:
    void onStart() {
        std::cout << "LoginActivity onStart" << std::endl;
    }
};

void startToActivity(BaseActivity * baseActivity) {
    baseActivity->onStart();
}

int main() {
    // 方式1
    HomeActivity * homeActivity = new HomeActivity();
    LoginActivity * loginActivity = new LoginActivity();
    startToActivity(homeActivity);
    startToActivity(loginActivity);

    // 方式2
    BaseActivity * baseActivity1 = new HomeActivity();
    BaseActivity * baseActivity2 = new LoginActivity();
    startToActivity(baseActivity1);
    startToActivity(baseActivity2);
    
    return 0;
}

静态多态

静态多态 : 编译期已经决定,调用哪个函数
重载(静态多态)

#include <iostream>

void add(int number1, int number2) {
    std::cout << number1 + number2 << std::endl;
}

void add(float number1, float number2) {
    std::cout << number1 + number2 << std::endl;
}

void add(double number1, double number2) {
    std::cout << number1 + number2 << std::endl;
}

int main() {
    add(1, 2);
    add(1.1f, 2.1f);
    add(1.2, 1.3);

    return 0;
}

纯虚函数(似java接口)

纯虚函数必须被继承,否则子类就是抽象类

#include <iostream>

class BaseActivity {
public:
    void onCreate() {
        getLayoutID();
        initView();
        initData();
    }

    // 纯虚函数必须被继承,否则子类就是抽象类

//    virtual std::string getLayoutID(); // 虚函数
    virtual std::string getLayoutID() = 0; // 纯虚函数
    virtual void initView() = 0; // 纯虚函数
    virtual void initData() = 0; // 纯虚函数
};

// 如果没有重新父类的纯虚函数,自己就相当于 抽象类
class MainActivity : public BaseActivity {
    std::string getLayoutID() {
        return "xxxxx";
    }
    void initView() {}
    void initData() {}
};

int main() {
    MainActivity mainActivity;

    return 0;
}

全纯虚函数(似java接口)

#include <iostream>

class Student {
public:
    std::string name;
    int age;
};

class IStudent {
public:
    virtual void insert(Student student) = 0; // 纯虚函数
    virtual void query(Student student) = 0; // 纯虚函数

};

// 如果没有重新父类的纯虚函数,自己就相当于 抽象类
class Student_Impl : public IStudent {
public:
    void insert(Student student) {
        std::cout << "insert" << std::endl;
    }
    void query(Student student) {
        std::cout << "query" << std::endl;
    }
};

int main() {
    Student * student = new Student();
    student->name = "zhangSan";
    student->age = 23;

    Student_Impl studentImpl;
    studentImpl.insert(*student);
    
    return 0;
}

回调

#include <iostream>

class ILoginResponse {
public:
    virtual void success(int code, char * msg) = 0; // 纯虚函数
    virtual void error(int code, char * msg) = 0; // 纯虚函数
};

class ILoginResponseImpl : public ILoginResponse {
public:
    void success(int code, char * msg) {
        std::cout << code << ", " << msg << std::endl;
    }
    void error(int code, char * msg) {
        std::cout << code << ", " << msg << std::endl;
    }
};

void loginAction(std::string userID, std::string pwd, ILoginResponse & callback) {
    if (userID.empty() || pwd.empty()){
        std::cout << "userID | pwd is empty!" << std::endl;
        return;
    }
    if (userID == "zhangSan" && pwd == "123") {
        callback.success(0, "login success");
    } else {
        callback.success(-1, "login fail");
    }
}

int main() {
    ILoginResponseImpl callback;
    loginAction("zhangSan", "12312", callback);

    return 0;
}

this

#include <iostream>

class People {
private:
    char * name;

public:
    static int id;

public:
    void setName(char * name){
        this->name = name;
    }
    char * getName(){
        return this->name;
    }
};

// 再实现
int People::id = 1001;

int main() {
    People people1;
    people1.setName("zhangSan1");
    people1.id = 2002;

    People people2;
    people2.setName("zhangSan2");
    people2.id = 3003;

    std::cout << "people1.getName(): " << people1.getName() << ", id: " << people1.id << std::endl;
    std::cout << "people1.getName(): " << people2.getName() << ", id: " << people2.id << std::endl;
    std::cout << "People::id: " << People::id << std::endl;

    // 静态区,没有this的区分

    return 0;
}

//people1.getName(): zhangSan1, id: 3003
//people1.getName(): zhangSan2, id: 3003
//People::id: 3003

static

#include <iostream>

class People {
public:
    char * name;
    // 报错
//    static int age = 9;
    // 正确  先声明
    static int age;

    void update1() {
        age += 1;
    }
    static void update2() {
        age += 1;
    }
};

// 再实现
int People::age = 9;

int main() {
    People people;
    std::cout <<  People::age << std::endl;  // 9

    People::update2();  // 静态函数
    people.update2();  // 对象名.静态函数 (一般是使用::调用静态成员)

    people.update1();  // 普通函数
    std::cout <<  People::age << std::endl;  // 12

    return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-19 07:48:45  更:2021-09-19 07:48:59 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 23:16:47-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码