c++类和对象

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c++类和对象

Huang Jason   2022-05-24 我要评论

什么是类?

一系列事物的抽象,对于c++而言,万事万物都可以是类。

类包括:属性+行为

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

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

什么是对象?

类的具体化,类的实例化,抽象->具象;

类的特点:封装、继承、派生、多态。

类的定义

创建方法:class

class 类名{

//权限限定词

public:

protected://保护属性

private://当不做继承时,数据成员写成私有属性

};//一定有一个分号

权限限定词作用:类外只能访问public属性下面的东西(接口),类外访问类中数据只能通过对象访问(static成员除外)

若类中申明类外实现,需要用类名限定(告诉别人函数属于哪个类的)

没有写在权限限定词下的属性,默认私有属性。

权限限定词只是限定类外对类中的访问,对类内无权限之分。

结构体中默认属性是共有属性

创建对象

普通对象、对象数组(使用较少)

#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();
	}
	MM* p = new MM;
	p->initData("张三", 18);
	p->print();
	delete p;
	p = nullptr;
	return 0;
}
#include <iostream>
#include <string>
using namespace std;
class GirlFriend
{
	void print() 
	{
		cout << "不在限定词下的属性" << endl;
		cout << "默认为私有属性" << endl;
	}
public:
	//共有属性
	//成员函数
	//类中实现函数
	void  printData() 
	{
		cout << m_name << "\t" << m_age << endl;
	}
	//为了访问不能访问的部分,通常提供一些接口
	void  initData(string name, int age);
protected:
	//保护属性
	//数据成员
	string m_name;
private:
	//当前类不做继承处理,数据成员写成私有属性
	int m_age;
};
//类外实现类中函数,需要类名限定,告诉别人这个函数是哪里来的
void GirlFriend::initData(string name,int age)  
{
	//Lisa.initData("Lisa", 19);  name="Lisa" age=19
	m_name = name;    //Lisa.m_name=Lisa
	m_age = age;	  //Lisa.m_age=19;
	//mm.initData("MM", 29);   name="MM" age=29
	//mm.m_name=MM;
	//mm.age=29
}
struct MM 
{
	int num;   //默认属性是公有属性
protected:
	string name;
private:
	int age;
};
void testMM() 
{
	//MM mm = { 1001,"name",28 };
	MM mm;
	mm.num = 103;
	//mm.name = "Ilove";
	//mm.age = 13;
}
int main()
{
	GirlFriend  Lisa;
	Lisa.initData("Lisa", 19);
	Lisa.printData();
	//类外只能访问public
	//Lisa.m_name = "Lisa";
	//Lisa.m_age = 18;
	GirlFriend mm;
	mm.initData("MM", 29);
	mm.printData();
	//mm.print();  --->不能访问私有属性
 
	return 0;
}

成员访问(初始化)

1.提供共有接口来初始化数据(传参)

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

#include <iostream>
#include <string>
using namespace std;
class MM
{
public:
	//传参
	void initData(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
	//返回引用
	string& getName() 
	{
		return m_name;
	}
	int& getAge() 
	{
		return m_age;
	}
	void print() 
	{
		cout << m_name << "\t" << m_age << endl;
	}
protected:
	//默认初始化
	string m_name="默认值";
	int m_age=0;
	//不做初始化是一个垃圾值
};
 
int main() 
{
	MM girl;
	girl.initData("girl", 19);
	girl.print();
 
	MM mm;
	mm.getName() = "mm";
	mm.getAge() = 18;
	mm.print();
 
	MM boy;
	boy.print();
 
	return 0;
}

c++有头链表与c对比:

#include <iostream>
#include <string>
using namespace std;
#if 0
struct Node
{
	int data;
	struct N ode* next;
};
struct Node* createList() 
{
	Node* headNode = new Node;
	headNode->next = nullptr;
	return headNode;
}
struct Node* createNode(int data) 
{
	Node* newNode = new Node;
	newNode->data = data;
	newNode->next = nullptr;
	return newNode;
}
void insertData(Node* headNode, int data) 
{
	Node* newNode = createNode(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 = createList();
	insertData(list, 10);
	insertData(list, 20);
	printList(list);
}
#endif
 
#if 0
struct Node 
{
	int data;
	Node* next;
};
class List 
{
public:
	void createList() 
	{
		headNode = new Node;
		headNode->next = nullptr;
	}
	void insertData(int data) 
	{
		Node* newNode = new Node;
		newNode->data = data;
		newNode->next = nullptr;
 
		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();
}
#endif 
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 list;
    //list.insertList(10);
    List* pList = new List;		//C++第一步:创建对象
	pList->createList();
	pList->insertData(10);
	pList->insertData(20);
	pList->printList();
}
int main() 
{
	//testListC();
	testList2();
	return 0;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们