Day_03 C++ 类和对象

        C++ 在 C 语言的基础上增加了面向对象编程,类是 C++ 的核心特性,通常被称为用户定义的类型。 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。 类中的数据和方法称为类的成员。

类和对象的基本概念

  • 什么是类?一系列事物的抽象,万物皆可为类

    • 类是有两部分组成: 属性(数据成员)    行为(成员函数)

    • 属性: 事物的特征--->数据类型描述

    • 行为: 事物的操作--->函数描述

  • 什么是对象: 类的具体化,类的实例化.

  • 类的特点: 封装,继承/派生,多态

类的定义

类的关键词 class  权限限定词 public   protected    private

public:公有属性,类外可以通过 对象 直接访问public下的属性和行为习惯把 public属性 叫做类                 外的接口。

protected和private权限限定词下的属性和行为都是不能再类外直接访问的 C++新标准中,可以直接给类中的数据成员初始化。

但是 可以通过public设置接口来实现类外的访问和修改  对于protected或者private中的函数 ,也可以设置public接口去调用。

#include<iostream>
#include<string>
using namespace std;

class student{
public:
	void printData(){
		cout << "姓名:" << stu_name << " " << "年龄:" << stu_age << " " << "学号:" << stu_num << " " << endl;
	}
	int& changeAge(){
		return stu_age;
	}
	string& changeName(){
		return stu_name;
	}
	string& changeNum(){
		return stu_num;
	}
protected:
	string stu_name="默认名称";
	string stu_num="默认学号";
	int stu_age=0;

private:

};

int main(){
	student* stu = new student;

    //通过public下的函数接口来访问stu中的成员数据
	stu->changeName() = "小白";
	stu->changeNum() = "13141";
	stu->changeAge() = 20;
	return 0;
}

class类不加权限限定词的话 默认是 private 类型

类和结构体区别不大,在一定程度上,类可以当作结构体。 只不过结构体中不加权限限定词,数据成员和成员函数默认是 public 类型。

结构体中也可以加权限限定词,注意,在加了private或者protected限定词后,在他们下面定义的数据成员和成员函数就不能直接访问了。如果想访问,可以参考类中设置接口

#include<iostream>
#include<string>
using namespace std;
struct Node{
public:
	int& getnum(){
		return num;
	}
	void print(){
		cout << num << endl;
	}
private:
	int num;
	string name;
};
int main(){

	Node node;
	node.getnum() = 20;
	node.print();
	return 0;
}

        总结

  • 权限限定 作用

    • 类外只能访问public属性下面的 东西,习惯把 public属性 叫做类外的接口

      • 类外访问 类中的数据,只能通过对象访问,当然static成员除外

    • protected和private 类外都不可以访问 ,但是可以提供共有接口间接访问

    • 默认属性(没有写在权限限定词下的属性)是 私有属性

    • 权限限定词,只是用来限定类外的访问,并不是限定中的访问

    • protected和private 有区别 ,继承有 区别,对类外 都是不可以访问

  • C++结构体 在一定程序可以直接 当作是类

    • 默认属性是公有属性

对象创建

  • 普通对象

  • 对象数组

  • new 一个对象

#include <iostream>
#include <string>
using namespace std;
class MM 
{
public:
	void print() 
	{
		cout << name << "\t" << age << endl;
	}
	void initData(string nname,int nage) 
	{
		name = nname;
		age = nage;
	}

protected:
	//新标准,可以在类中给数据直接初始化
	string name="默认值";
	int age=0;
};
int main() 
{
    //普通对象
	//没有写构造函数的情况下,和C语言的创建方式是一样的
	MM  mm;
	mm.print();			//没有初始化数据

    //对象数组
	MM mmArray[4];		//一般很少用对象数组
	//mmArray[0]----mmArray[3]
	//数组: 多个变量名有规律,内存连续的变量的集合
	for (int i = 0; i < 4; i++) 
	{
		mmArray[i].initData(string("name") + to_string(i), i + 19);
		mmArray[i].print();
	}

    //new申请对象
	MM* p = new MM;
	p->initData("张三", 18);
	p->print();
	delete p;
	p = nullptr;
	return 0;
}

成员访问(初始化)

  • 通过提供 公有接口传参的方式初始化数据

    #include<iostream>
    #include<string>
    using namespace std;
    class student{
    public:
        //传参的方式
    	void initData(string name, int age){
    		stu_name = name;
    		stu_age = age;
    	}
    	void printData(){
    		cout << stu_name << "  " << stu_age << endl;
    	}
    protected:
    	string stu_name;
    	int stu_age;
    private:
    };
    int main(){
    	student* std = new student;
    	std->initData("小红", 20);
    	std->printData();
    
    	return 0;
    }

  • 通过提供 公有接口返回值的方式初始化数据

  • #include<iostream>
    #include<string>
    using namespace std;
    class student{
    public:
    	//接口返回值的方式访问成员数据
    	int& getAge(){
    		return stu_age;
    	}
    	string& getName(){
    		return stu_name;
    	}
    	void printData(){
    		cout << stu_name << "  " << stu_age << endl;
    	}
    protected:
    	string stu_name;
    	int stu_age;
    private:
    };
    int main(){
    	student* std = new student;
    	std->getName()="小红";
    	std->getAge() = 20;
    	std->printData();
    
    	return 0;
    }
  • 默认初始化

  • #include<iostream>
    #include<string>
    using namespace std;
    class student{
    public:
    	void printData(){
    		cout << stu_name << "  " << stu_age << endl;
    	}
    protected:
    	//初始化的方式
    	string stu_name = "默认名称";
    	int stu_age = 0;
    private:
    };
    int main(){
    	student* std = new student;
    	std->printData();
    
    	return 0;
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值