C++类和对象

C++类和对象


前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、类和对象的基本概念

1.什么是类

一系列事物的抽象,万物皆可为类 ,比如与我们现实生活中接近的饮品,饮品有果汁、汽水、咖啡等等 
我们将这些饮品总称为饮品,饮品也就是我们所说的类
类有两部分组成:属性,行为
属性:事物的特征-->用数据类型描述
行为:事物的操作--->用函数描述

2.什么是对象

类的具体化,类的实例化.
通过类创造一个对象将事物具体化实例化

2.类的特点

封装,继承/派生,多态

二、类的定义

1.创建语法

class 类名
{
//public 权限限定词
public:
protected:
private:
}; //一定要有

2.权限限定

类外只能访问public属性下面的东西
	类外访问类中的数据,只能通过对象访问,习惯把public属性叫做类外的接口
		类外访问类中的数据,只能通过对象访问,当然static成员除外
protected和private类外都不可以访问,但是可以提供共有接口间接访问 
#include <iostream>
#include <string>
using namespace std;
class student
{
public:	//共有属性
	//成员函数 类中实现函数
	void printData()
	{
		cout << s_name <<"\t"<< s_age << endl;
	}
	//为了访问不能访问的部分,通常提供一些接口
	void initData(string name, int age);
protected:	//保护属性
	//数据成员
	string s_name;
	
private:	//当前类不做继承处理,数据成员写成私有属性
	int s_age;
};
//类外实现类中函数,需要类名限定,告诉别人这个函数是哪里来的
void student::initData(string name, int age)
{
	s_name = name;
	s_age = age;
}
int main()
{
	//m_name = "李四"; 错误 类外只能访问public属性下面的东西
	student lisi;
	lisi.initData("lisi", 19);
	lisi.printData();
	while (1);
	return 0;
}



运行结果:
在这里插入图片描述
默认属性(没有写在权限限定词下的属性)是私有属性

#include <iostream>
#include <string>
using namespace std;
class student
{
	void print()
	{
		cout << "不再限定词下的属性" << endl;
		cout << "默认为私有属性" << endl;
	}
public:	//共有属性
	void initData(string name, int age);
protected:	//保护属性
	//数据成员
	string s_name;
	
private:	//当前类不做继承处理,数据成员写成私有属性
	int s_age;
};

int main()
{
	
	//student.print(); 错误不能访问私有属性
	while (1);
	return 0;
}
权限限定词,只是用来限定类外的访问,并不是限定类中的访问
#include <iostream>
#include <string>
using namespace std;
struct student
{
	int num;	//默认属性是公用属性
	void print()
	{
		cout<<num<<endl;
	}
protected:
	string name;
private:
	int age;
};
void teststudent()
{
	student stu;
	stu.num = 103;
}
int main()
{

	while (1);
	return 0;
}

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

3.C++结构体在一定程序上可以直接当作类

struct student
{
int num; //默认属性是公用属性
protected:
string name;
private:
int age;
};
void teststudent()
{
student stu;
stu.num = 103;
}

三、对象创建

1.普通对象

2.对象数组

3.new一个对象

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

class student
{
public:
	void print()
	{
		cout << name << "\t" << age<< endl;
	}
	void initData(string s_name,int s_age)
	{
		name = s_name;
		age = s_age;
	}
protected:
	string name="默认值";
private:
	int age=0;
};
int main()
{
	//没有写构造函数的情况下,和C语言的创建方式是一样的
	student stu;
	stu.print();		//没有初始化数据
	student stuArray[5]; //一般很少用对象数组
	//stuArray[0]--stuArray[4]
	//数组:多个变量名有规律,内存连续的变量的集合
	for (int i = 0; i < 5; i++)
	{
		stuArray[i].initData(string("name") + to_string(i), i + 19);
		stuArray[i].print();
	}
	student *p = new student;
	p->initData("lisi", 28);
	p->print();
	delete p;
	p = nullptr;
	while (1);
	return 0;

}

运行结果:
在这里插入图片描述

四、成员访问(初始化)

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

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

class student
{
public:
	//传参
	void initData(string name, int age)
	{
		s_name = name;
		s_age = age;
	}
	void print()
	{
		cout << s_name << "\t" << s_age<<endl;
	}
protected:
	string s_name;
	int s_age;
private:
};

int main()
{
	student stu;
	stu.initData("zhangsan", 18);
	stu.print();
	while (1);
	return 0;
}

运行结果:
在这里插入图片描述

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

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

class student
{
public:
	string& getName()
	{
		return s_name;
	}
	int& getAge()
	{
		return s_age;
	}
	void print()
	{
		cout << s_name << "\t" << s_age<<endl;
	}
protected:
	string s_name;
	int s_age;
private:
};

int main()
{
	student stu;
	stu.getName() = "lisi";
	stu.getAge() = 19;
	stu.print();
	while (1);
	return 0;
}

运行结果:

在这里插入图片描述

3.默认初始化

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

class student
{
public:
	void print()
	{
		cout << s_name << "\t" << s_age<<endl;
	}
protected:
	string s_name="默认值";
	int s_age=0;
private:
};

int main()
{
	student stu;
	stu.print();
	while (1);
	return 0;
}

运行结果:
在这里插入图片描述

面对对象的编程方式

#include <iostream>
#include <stdio.h>
using namespace std;

//C语言链表
//struct Node
//{
//	int data;
//	struct Node* next;
//};
//struct Node* CreatList()
//{
//	Node* headNode = new Node;
//	headNode->next = nullptr;
//	return headNode;
//}
//
//struct Node* CreatNode(int data)
//{
//	Node* newNode = new Node;
//	newNode->data = data;
//	newNode->next = nullptr;
//	return newNode;
//}
//void insertData(Node* headNode, int data)
//{
//	Node* newNode = CreatNode(data);
//	newNode->next = headNode->next;
//	headNode->next = newNode;
//}
//void printList(Node* headNode)
//{
//	Node* pMove = headNode->next;
//	while (pMove != nullptr)
//	{
//		cout << pMove->data << " ";
//		pMove = pMove->next;
//	}
//	cout << endl;
//}
//void testListC()
//{
//	Node *list = CreatList();
//	insertData(list, 10);
//	insertData(list, 20);
//	printList(list);
//
//}
//C++链表
//struct Node
//{
//	int data;
//	Node* next;
//};
//class List
//{
//public:
//	void creatList()
//	{
//		headNode = new Node;
//		headNode->next = nullptr;
//	}
//	void insertData(int data)
//	{
//		Node* newNode = new Node;
//		newNode->data = data;
//		newNode->next = headNode->next;
//		headNode->next = newNode;
//	}
//	void printList()
//	{
//		Node* pMove = headNode->next;
//		while (pMove != nullptr)
//		{
//			cout << pMove->data << " ";
//			pMove = pMove->next;
//			cout << endl;
//		}
//	}
//protected:
//	Node* headNode;
//};
//void testList1()
//{
//	List *plist = new List; //C++第一步一定是创建对象
//	plist->insertData(10);
//	plist->insertData(20);
//	plist->printList();
//
//}
//类创建链表
class Node
{
public:
	Node*& getNext()
	{
		return next;
	}
	int& getData()
	{
		return data;

	}
protected:
	int data;
	Node* next;
};
class list
{
public:
	void createList()
	{
		headNode = new Node;
		headNode->getNext() = nullptr;
	}
	void insertData(int data)
	{
		Node* newNode = new Node;
		newNode->getNext() = nullptr;
		newNode->getData() = data;
		newNode->getNext() = headNode->getNext();
		headNode->getNext() = newNode;
	}
	void printList()
	{
		Node* pMove = headNode->getNext();
		while (pMove != nullptr)
		{
			cout << pMove->getData() << "\t";
			pMove = pMove->getNext;
			cout << endl;
		}

	}
protected:
	Node* headNode;

};
void testList2()
{
	list *plist = new list; //C++第一步一定是创建对象
	plist->createList();
	plist->insertData(10);
	plist->insertData(20);
	plist->printList();

}
int main()
{
	//testList1();
	testList2();
	 
	while (1);
	return 0;
}

总结

从本次学习中了解到C++中类和对象的基本概念并学习到如何创造类和对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值