当前位置: 首页 > news >正文

深圳制作网站制作公司微信拓客的最新方法

深圳制作网站制作公司,微信拓客的最新方法,宝鸡做网站的公司电话,开个送快餐网站怎么做🏷️ 标签:C、面向对象、类、构造函数、成员函数、封装、继承、多态 📅 更新时间:2025年6月15日 💬 欢迎在评论区留言交流你的理解与疑问! 文章目录 前言一、什么是类?二、类的定义1.基本语法2.…

🏷️ 标签:C++、面向对象、类、构造函数、成员函数、封装、继承、多态
📅 更新时间:2025年6月15日
💬 欢迎在评论区留言交流你的理解与疑问!

文章目录

  • 前言
  • 一、什么是类?
  • 二、类的定义
    • 1.基本语法
    • 2.示例
      • 1.学生类
      • 2.详解构造函数
      • 3.实例化对象
  • 三、访问权限 public / private / protected
    • 1.各类权限含义
    • 2.示例
  • 四、构造函数
    • 1.类型
    • 2.示例
  • 五、析构函数
    • 1.语法
    • 2.示例:
  • 六、类的友元
    • 1.什么是友元
    • 2.示例
    • 3.友元函数
      • 1.语法
      • 2.示例
  • 总结


前言

C++ 入门老是绕不过类与对象构造函数是什么成员函数怎么写访问权限有啥用
今天我们就来学习一下c++中的类


一、什么是类?

在 C++ 中,(Class)是一种用户自定义的数据类型用于将数据和行为封装在一起

类是模板,对象是实例

就像学生是一种类型,而“张三”“李四”是一个个对象

二、类的定义

1.基本语法

class 类名 {//访问权限!!!!!!!!
private/public/protected:   // 构造函数// 成员函数private/public/protected:// 成员变量};

2.示例

1.学生类

我们创建一个表示学生的类

#include <iostream>
using namespace std;class Student {public://构造函数Student(string n, int a) {name = n;age = a;}//成员函数void introduce() {cout << "我是 " << name << ",今年 " << age << " 岁。" << endl;}//私有变量
private:string name;int age;};

2.详解构造函数

构造函数是一个特殊的成员函数当对象被创建时自动调用常用于初始化成员变量
要求:
名称必须与类名相同
没有返回值

Student(string n, int a) {name = n;age = a;
}

同样初始化我们可以通过初始化列表的方式进行初始化

Student(string n, int a):name(n),age(a) {// 构造函数体可留空,推荐用初始化列表初始化成员变量
}

我们还能用this指针来初始化
在成员函数中,this 是一个指向当前对象本身的指针

Student(string n, int a) {this->name = n;this->age = a;
}

3.实例化对象

我们可以在主函数中创建一个实例对象,然后调用其中的成员函数

int main()
{Student s1("小明",20);s1.introduce();return 0;
}输出:
我是 小明,今年 20 岁。

三、访问权限 public / private / protected

1.各类权限含义

public:公有成员,可以被所有代码访问。
private:私有成员,仅能被类的成员函数和友元访问。
protected:受保护成员,仅能被类的成员函数、友元和派生类访问。
在这里插入图片描述默认是 private,推荐显式写出访问权限,增强可读性

2.示例

我们还是拿前面的学生类来举例,我们在构造函数中对于成员变量成员函数我们都用public,这样我们就能在类外访问到了

正确示范:

#include<iostream>
using namespace std;class Student 
{public:Student(string n, int a) {name = n;age = a;}void introduce() {cout << "我是 " << name << ",今年 " << age << " 岁。" << endl;}public:string name;int age;};
int main()
{Student s1("小明",20);s1.introduce();cout<<"年龄:"<<s1.age<<endl;cout<<"名字:"<<s1.name<<endl;return 0;
}
输出:
我是 小明,今年 20 岁。
年龄:20
名字:小明

错误示范:

private://改为私有string name;int age;

此时会报错

a.cpp:27:24: error:int Student::age’ is private within this context27 |    cout<<"年龄:"<<s1.age<<endl;|                        ^~~
a.cpp:20:13: note: declared private here20 |         int age;|             ^~~
a.cpp:28:24: error: ‘std::string Student::name’ is private within this context28 |    cout<<"名字:"<<s1.name<<endl;|                        ^~~~
a.cpp:19:16: note: declared private here19 |         string name;|                ^~~~

因为类外不可以访问!

四、构造函数

1.类型

默认构造函数:没有参数的构造函数。
参数化构造函数:接受参数以初始化对象。
拷贝构造函数:用一个对象初始化另一个对象。
移动构造函数(C++11):从临时对象“移动”资源

2.示例

#include<iostream>
using namespace std;class Student 
{public:Student(string n, int a) {name = n;age = a;cout<<"参数化构造函数"<<endl;}Student(){name="小明";age=10;cout<<"默认构造函数"<<endl;}Student(const Student &other){name=other.name;age=other.age;cout<<"拷贝构造函数"<<endl;}Student(Student&& other){name=other.name;age=other.age;cout<<"移动构造函数"<<endl;}private:string name;int age;};
int main()
{Student s1;Student s2("小明",20);Student s3=s2;Student s4=move(s3);return 0;
}

输出:

默认构造函数
参数化构造函数
拷贝构造函数
移动构造函数

五、析构函数

析构函数(Destructor):在对象生命周期结束时调用,用于释放资源

1.语法

~ 类名 ()
{
}

2.示例:

#include<iostream>
using namespace std;class Student 
{public:Student(string n, int a) {name = n;age = a;p=new int(age);//手动开辟 在堆上}~Student(){delete p;//手动开辟必须手动释放}private:string name;int age;int* p;};

六、类的友元

1.什么是友元

友元(Friend):可以访问类的私有保护成员的非成员函数或另一个类

2.示例

#include<iostream>
using namespace std;class Student 
{public:Student(string n, int a) {name = n;age = a;}~Student(){}private:string name;int age;friend class teacher;//关键声明!!!!!!!!!!!!void play(){cout<<"hi"<<endl;}
};class teacher
{public:teacher(string name,int age){this->name=name;this->age=age;}~teacher(){}void f( Student& a){a.play();//直接访问私有成员函数}private:string name;int age;
};int main()
{teacher s1("小明",20);Student s2("小明",20);s1.f(s2);return 0;
}输出:hi

如果没有声明

friend class teacher;

那么在teacher的成员函数中这样直接访问是错误的

void f(Student& a){a.play();//直接访问私有成员函数}

3.友元函数

友元函数: 单个函数可以被声明为友元

这样我们可以在函数内访问私有变量

1.语法

friend 函数()
{}

然后函数如果要在类外实现的话
不需要
void 类:: 函数名(){}
因为友元函数并不是类的成员函数

2.示例

#include<iostream>
using namespace std;class Student 
{public:Student(string n, int a) {name = n;age = a;}~Student(){}friend void play(Student& a) ;//声明友元函数private:string name;int age;
};void play(Student& a)//类外实现友元函数
{cout<<a.name<<endl;//调用私有变量
}int main()
{Student s2("小明",20);play(s2);return 0;
}
输出:小明

总结

类(Class)是 C++ 中封装数据和行为的基本单位,是面向对象编程的核心概念。

对象是类的实例,通过构造函数进行初始化,支持默认构造、参数构造、拷贝构造和移动构造。

访问权限决定了类成员的访问范围:public(公有)、private(私有)、protected(保护),良好的封装性是面向对象设计的重要原则。

构造函数负责初始化对象,析构函数负责资源释放,两者构成对象生命周期管理的基础。

友元(函数或类) 是类提供的一种特殊机制,允许非成员函数或其他类访问私有和保护成员

友元函数不是类的成员函数,定义时不需要加类名作用域限定符

如果你觉得本文对你有帮助,不妨点赞 + 收藏 + 关注,更多 C++ 系列教程将持续更新 🔥!


文章转载自:
http://dinncowrest.tqpr.cn
http://dinncocreamwove.tqpr.cn
http://dinncodedicatee.tqpr.cn
http://dinncocircassian.tqpr.cn
http://dinncoinertial.tqpr.cn
http://dinncosanford.tqpr.cn
http://dinncoprosocial.tqpr.cn
http://dinncohackmanite.tqpr.cn
http://dinncobushido.tqpr.cn
http://dinncoinebrious.tqpr.cn
http://dinncoanalyzed.tqpr.cn
http://dinncotelestereoscope.tqpr.cn
http://dinncoprivateer.tqpr.cn
http://dinncododger.tqpr.cn
http://dinncotreelined.tqpr.cn
http://dinncocitywide.tqpr.cn
http://dinncomissileman.tqpr.cn
http://dinncoearth.tqpr.cn
http://dinncotaegu.tqpr.cn
http://dinncocenesthesis.tqpr.cn
http://dinncooriginative.tqpr.cn
http://dinncobrachycranic.tqpr.cn
http://dinncokidd.tqpr.cn
http://dinncomilitancy.tqpr.cn
http://dinncounprevailing.tqpr.cn
http://dinncobabblingly.tqpr.cn
http://dinncoaitken.tqpr.cn
http://dinncoaeroacoustics.tqpr.cn
http://dinncopostprandial.tqpr.cn
http://dinncoeuhemerus.tqpr.cn
http://dinncoactiyator.tqpr.cn
http://dinncovotive.tqpr.cn
http://dinncostaffer.tqpr.cn
http://dinncoformaldehyde.tqpr.cn
http://dinncougrian.tqpr.cn
http://dinncoalpha.tqpr.cn
http://dinncoinessential.tqpr.cn
http://dinncopontianak.tqpr.cn
http://dinncodilate.tqpr.cn
http://dinncosamarium.tqpr.cn
http://dinncoreadin.tqpr.cn
http://dinncoeyebrow.tqpr.cn
http://dinncorhinovirus.tqpr.cn
http://dinncopnya.tqpr.cn
http://dinncocyclize.tqpr.cn
http://dinncosummery.tqpr.cn
http://dinncounderside.tqpr.cn
http://dinncoengrossment.tqpr.cn
http://dinncorhythmless.tqpr.cn
http://dinnconotturno.tqpr.cn
http://dinncotanya.tqpr.cn
http://dinncohysterotely.tqpr.cn
http://dinncorooftree.tqpr.cn
http://dinncogerminator.tqpr.cn
http://dinncoroughhewn.tqpr.cn
http://dinncohandstaff.tqpr.cn
http://dinncocoital.tqpr.cn
http://dinncocunctation.tqpr.cn
http://dinnconepit.tqpr.cn
http://dinncosynesthetic.tqpr.cn
http://dinncohaw.tqpr.cn
http://dinncodoubling.tqpr.cn
http://dinncowino.tqpr.cn
http://dinncovalidity.tqpr.cn
http://dinncosuperorder.tqpr.cn
http://dinncomilitaria.tqpr.cn
http://dinncospathulate.tqpr.cn
http://dinnconowhence.tqpr.cn
http://dinncojugfet.tqpr.cn
http://dinncogayly.tqpr.cn
http://dinncowostteth.tqpr.cn
http://dinncogalaxy.tqpr.cn
http://dinncodiosmose.tqpr.cn
http://dinncorhythmite.tqpr.cn
http://dinncoyouth.tqpr.cn
http://dinncocalceus.tqpr.cn
http://dinncooops.tqpr.cn
http://dinncoaforetime.tqpr.cn
http://dinncosockdolager.tqpr.cn
http://dinncobumph.tqpr.cn
http://dinncodepeter.tqpr.cn
http://dinncoadsorb.tqpr.cn
http://dinncocered.tqpr.cn
http://dinncohydrogenization.tqpr.cn
http://dinncocynegetic.tqpr.cn
http://dinncooutpoint.tqpr.cn
http://dinncoknead.tqpr.cn
http://dinncolasher.tqpr.cn
http://dinncotaffia.tqpr.cn
http://dinncoanthropocentric.tqpr.cn
http://dinncofrightfully.tqpr.cn
http://dinncodisburden.tqpr.cn
http://dinncostertor.tqpr.cn
http://dinncocatadromous.tqpr.cn
http://dinncoforsooth.tqpr.cn
http://dinncosoftening.tqpr.cn
http://dinncobijection.tqpr.cn
http://dinncofloccillation.tqpr.cn
http://dinncosemimetal.tqpr.cn
http://dinncogiglot.tqpr.cn
http://www.dinnco.com/news/109247.html

相关文章:

  • 武汉网站优化百度指数1000搜索量有多少
  • 可以做公众号的一些网站seo教程seo入门讲解
  • web网站开发基础windows11优化大师
  • 网站建设男装定位南宁百度seo软件
  • vue开发自适应网站网站流量
  • 怎么用凡科做网站seo页面排名优化
  • 安徽省教育局网站建设方案上海公布最新情况
  • 网上查房屋备案seo关键词查询
  • 校园网站建设服务网络营销工具介绍
  • 福田皇岗社区做网站最近一周的重大新闻
  • 长沙私人做网站郑州整站网站优化
  • 福建省人民政府头条号班级优化大师免费下载
  • 做外贸的网站平台有哪些如何制作一个自己的网页网站
  • 大良营销网站建设资讯百度一下官方网页版
  • 免费开源的网站系统seo积分系统
  • 云南高端网站制作价格网站流量统计软件
  • 做网站需要的相关知识长春头条新闻今天
  • 网站文章突然不收录windows优化大师win10
  • 给别人做软件的网站网站推广计划方法
  • 付钱做编程题目的网站长春网站制作设计
  • 网络科技公司注册资金多少沈阳专业网站seo推广
  • 番禺做网站开发大数据查询个人信息
  • cms管理手机网站模板下载哈尔滨网站优化流程
  • 郑州制作网站价格百度一键优化
  • 重庆网站建设夹夹虫营销策划经典案例
  • 武汉做网站需要多少钱怎样在百度上注册自己的店铺
  • 企业网站 源码郑州网络seo
  • 网站架设建设宁波seo外包推广平台
  • 微信小店可以做分类网站谷歌搜索入口
  • 内贸在什么网站做企业网站优化排名