C++面向对象

目录

一 面向对象简介

一 面向对象实例代码演示

二 拷贝构造函数实例代码

三 友元函数使用示范

四 内联函数代码示范

五 类指针使用示范

六 静态成员变量与函数的使用


一 面向对象简介

面向对象是一种哲学思想,其哲理及意义也是非常简单:类似佛家的一花一世界的思想。

在编程界,面向对象的思想已经遍地开花,无处不在,那么面向对象具体在程序中是以类的方式进行体现的。

类是对现实世界的抽象,类中会定义成员和属性,对应与显示世界的行为与特征。

本质上类属于一种自定义的类型,其功能非常强大,潜力无穷。

类的四个特征: 封装、继承、多态、抽象 。

一 面向对象实例代码演示

student头文件

//
// Created by 11010 on 2023/4/7.
//

#ifndef CPP_10_DEFINECLASS_STUDENT_H
#define CPP_10_DEFINECLASS_STUDENT_H
#include <string>

using namespace std;
//定义一个Student类
class Student {

//在类里面使用修饰符定义几个属性,public表示该属性公开(任何人均可访问),

private外部均不可以访问,protected只能继承类访问
public:
    string name;
    int age;
    string  hobby;

    //定义1个方法用于打印学生信息
    void StudentInfo(Student s);

    /*申明类的构造函数: 构造函数名称与类名一致,没有返回值但可以携带参数,
    在定义类对象或变量时被系统自动调用,一般用于初始化成员。*/
    Student(string name,int age,string hobby);  //带参数的构造方法
    Student(); //不带参数的构造方法
    Student(string name,int age); //定义两个参数,便于演示初始化列表的使用
    Student(int age); //定义一个有指针的参数的构造函数。便于演示拷贝构造函数的使用

    //申明析构函数: 名称与类名一致,前面以~开头,不能有参数和返回值,在删除对象时会被调用,用于在对象结束时做一些资源释放的工作。
    ~Student();


};


#endif //CPP_10_DEFINECLASS_STUDENT_H

Student源文件

//
// Created by 11010 on 2023/4/7.
//

#include "Student.h"
#include <iostream>

using namespace std;

//实现方法的定义
void Student::StudentInfo(Student s) {
    cout<<"学习信息为:"<<s.name<<","<<s.age<<","<<s.hobby<<endl;
}

//构造方法: 携带参数
Student::Student(string name,int age,string hobby) {
    //把当前的参数值赋给成员变量
    this->name=name;
    this->age=age;
    this->hobby=hobby;
    cout<<"有参构造方法执行了"<<endl;
}

//构造方法:没有参数
Student::Student() {
  cout<<"无参构造方法执行了"<<endl;
}

//使用初始化列表给成员参数赋值
Student::Student(string name, int age):name(name),age(age)
{
    this->hobby="打羽毛球";
}

//析构函数
Student::~Student() {
cout<<"析构函数被调用,当前对象所占内存资源已经被自动回收!"<<endl;
}

//携带指针参数,便于演示拷贝构造函数
Student::Student(int age) {
    this->age=age;
    this->name="指针";
    this->hobby="拷贝函数";
}

Main.cpp

#include <iostream>
#include "Student.h"  //在使用自定义类之前,需要导入对应的头文件

int main() {

    //使用自定义类 Student
    Student student; //定义一个类类型变量student,在创建student变量后默认会调用无参构造方法
    //给变量成员赋值
    student.name = "张三";
    student.age = 34;
    student.hobby = "打篮球";

    //调用类方法,输出学习信息
    student.StudentInfo(student);

    //访问类的成员
    cout << student.name << "," << student.age << "," << student.hobby << endl;

    //使用有参构造方法
    Student student1("小李", 22, "滑冰");
    student1.StudentInfo(student1);

    //使用有参构造方法: 使用初始化列表的构造方法
    Student student2("小明", 29);
    student2.StudentInfo(student2);

    //拷贝构造函数: 他是用于拷贝一个已有的对象来初始化一个新的对象的函数,
    // 如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。
    Student student3 = student; //编译器自动创建默认的拷贝构造函数,把student对象拷贝给student3
    student3.StudentInfo(student3);



    return 0;
}

二 拷贝构造函数实例代码

拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:

通过使用另一个同类型的对象来初始化新创建的对象。

复制对象把它作为参数传递给函数

复制对象,并从函数返回这个对象。

如果在类中没有定义拷贝构造函数,编译器会自行定义一个。如果类带有指针变量,并有动态内存分配,则它必须有一个拷贝构造函数。

Book.h

//
// Created by 11010 on 2023/4/7.
//

#ifndef CPP_10_DEFINECLASS_BOOK_H
#define CPP_10_DEFINECLASS_BOOK_H


class Book {

public:
    Book(int price); //简单构造函数
    Book(const Book &book); //拷贝构造函数
    void disValue(Book book); //显示值
    int getValue(); //获取私有成员值

private:
    int *ptr;
};


#endif //CPP_10_DEFINECLASS_BOOK_H

Book.cpp

//
// Created by 11010 on 2023/4/7.
//

#include "Book.h"
#include <iostream>

using namespace std;

//普通构造函数
Book::Book(int price) {
cout<<"给指针分配了内存,原型为Book::Book(int price)"<<endl;
//为指针分配内存
ptr=new int ;
*ptr=price; //把当前参数值赋给成员指针
}

//构造拷贝函数
Book::Book(const Book &book) {
cout<<"开始给指针拷贝值"<<endl;
ptr=new int ;
*ptr=*book.ptr;
}

//显示值
void Book::disValue(Book book) {
 cout<<"值为: "<<book.getValue()<<endl;
}

int Book::getValue() {
    return *ptr;
}

Main.cpp

 

Int main(){

Book book(1999);
    book.disValue(book);


    return 0;
}

三 友元函数使用示范

friendFunc.h

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_10_DEFINECLASS_FRIENDFUNC_H
#define CPP_10_DEFINECLASS_FRIENDFUNC_H
#include <iostream>
#include <string>

using namespace std;

//申明一个类
class friendFunc {
//写一些属性
public:
    string name;
    int age;
    //使用friend关键字申明友元函数: 友元函数不属于任何类,仅仅是可以访问friendFunc的任何成员
    friend void printInfo(friendFunc f);
    void setHobby(string hobby);

private:
    string hobby;
};


#endif //CPP_10_DEFINECLASS_FRIENDFUNC_H

friendFunc.cpp

//
// Created by 11010 on 2023/4/8.
//

#include "friendFunc.h"

//定义友元函数: 可以看到,被private修饰的成员均可访问
void printInfo(friendFunc f) {
 cout<<f.name<<"信息为:"<<","<<f.age<<","<<f.hobby<<","<<endl;
}

void friendFunc::setHobby(string hobby)
{
    this->hobby=hobby;
}

Main.cpp

  int main(){  

  //给类成员赋值
    friendFunc friendFunc;
    friendFunc.name="friendFunc";
    friendFunc.age=35;
    friendFunc.setHobby("打羽毛球"); //若是普通的访问方式,需要使用set方法来访问被private修饰的变量
    //使用友元函数访问类的任意成员
    printInfo(friendFunc);

    return 0;
}

四 内联函数代码示范

内联函数:是只有一条简单语句且用inline关键字的修饰的函数,内联函数会被编译器做优化处理:编译器会把该函数的代码副本放置在每个调用该函数的地方。这样会提高代码的执行效率。但是随之而来的问题是会提高程序的整体体积,以空间换时间。

#include <iostream>
#include "Student.h"  //在使用自定义类之前,需要导入对应的头文件
#include "Book.h"
#include "friendFunc.h"

//使用online关键字申明一个内联函数:执行内容简单,使用inline修饰,凡是出现内联函数调用的地方编译器均会赋值副本。整体上是以空间换取时间。
inline int max(int a,int b){
    return a>b ? a:b;
}


int main() {

   
    //使用内联函数
    int MAX=max(12,34);
    cout<<"较大的数是:"<<MAX<<endl;

    return 0;
}

this指针使用示范:

ThisDemo.h

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_10_DEFINECLASS_THISDEMO_H
#define CPP_10_DEFINECLASS_THISDEMO_H


class ThisDemo {

public:
    int age;
void printAge(int age);


};


#endif //CPP_10_DEFINECLASS_THISDEMO_H

ThisDemo.cpp

//
// Created by 11010 on 2023/4/8.
//

#include "ThisDemo.h"
#include <iostream>

using namespace std;

void ThisDemo::printAge(int age) {
 //每一个对象都能通过 this 指针来访问自己的地址。this 指针是所有成员函数的隐含参数.
 // 可以简单理解为this就是类本身的对象,那么就可以使用这个对象来访问类的成员。
    this->age=age;
    cout<<"ThisDemo-->age==="<< this->age<<endl;
}

Main.cpp

Int main(){ 

//使用this参与赋值的成员函数
    ThisDemo thisDemo;
    thisDemo.printAge(23);

    return 0;
}

五 类指针使用示范

类指针的使用与结构指针的使用基本一样。

//类指针的使用示范: 申明一个类指针,把book变量的地址赋予它
Book *bookPointer=&book;
//使用类指针访问类的成员
bookPointer->disValue(book);

六 静态成员变量与函数的使用

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_10_DEFINECLASS_STATICDEMO_H
#define CPP_10_DEFINECLASS_STATICDEMO_H


class staticDemo {
public:
    //使用static申明成员变量与函数: 静态成员为类所共享,且静态成员会一致存在,无论创建多少个类的对象均只有一个内存副本。
    static int count;
    static void printCount();
};


#endif //CPP_10_DEFINECLASS_STATICDEMO_H



//
// Created by 11010 on 2023/4/8.
//

#include "staticDemo.h"
#include <iostream>

using namespace std;

//在外部使用类访问修饰符:: 初始化静态成员变量
int staticDemo::count=0;


void staticDemo::printCount() {
    for (int i = 0; i < 100; ++i) {
        count++;
    }
   cout<<"总次数为:"<<count<<endl;
}









int main() {



   //创建多个对象,看看静态成员的内存地址是否会变化
   staticDemo demo,demo1,demo2,demo3;
   int a=demo.count+1;
   int b=demo1.count+2;
   int c=demo2.count+3;
   int d=demo3.count+4;

   cout<<"count的指针地址:"<<&(demo.count)<<","<<&(demo1.count)<<","<<&(demo2.count)<<","<<&(demo3.count)<<endl;
   //输出结果为: 0x7ff7e4779090,0x7ff7e4779090,0x7ff7e4779090,0x7ff7e4779090 ,可以发现四个对象的count始终只有一个副本
   cout<<"值变化:"<<a<<","<<b<<","<<c<<","<<d<<","<<endl;
   demo.printCount();
   demo1.printCount();
   demo2.printCount();
   demo3.printCount();

   //也可以直接访问public修饰的静态成员
   staticDemo::count=100;
   staticDemo::printCount();

    return 0;
}

源码clone地址: git@gitcode.net:XiaoWang_csdn/cpp_10_defineclass.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_shenbing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值