-
类和对象的基本概念
-
什么是类,一系列事物的抽象,万物皆可为类
-
类是有两部分组成: 属性 行为 -
属性: 事物的特征--->数据类型描述 -
行为: 事物的操作--->函数描述
-
什么是对象: 类的具体化,类的实例化. -
类的特点: 封装,继承/派生,多态
-
类的定义
- 类的创建
class 类名{
//权限限定词
public:
protected:
private:
};//不要遗漏;
-
权限限定 作用
-
类外只能访问public属性下面的 东西,习惯把 public属性叫做类外的接口
-
protected和private 类外都不可以访问 ,但是可以提供共有接口间接访问 -
默认属性(没有写在权限限定词下的属性)是私有属性 -
权限限定词,只是用来限定类外的访问,并不是限定中的访问 -
protected和private 有区别 ,继承有区别,对类外都是不可以访问 class student {
int m_score;//默认属性为私有属性
public:
void print() {};
protected:
string m_name;
int m_stuNum;
private:
};
void student::print() {
cout << m_name << " " << m_stuNum <<m_score<<" "<< endl;
}
-
C++结构体在一定程序可以直接当作是类
-
对象创建
-
普通对象 -
对象数组 -
new 一个对象 -
#include<iostream>
#include <string>
using namespace std;
class student {
int m_score = 0;//默认属性为私有属性
public:
void print();
protected:
//新标准,可以在类中给数据直接初始化
string m_name = "默认值";
int m_stuNum = 0;
private:
};
void student::print(){
cout << m_name << " " << m_stuNum <<" "<<m_score<< endl;
}
//创建对象
void CreateObject() {
//普通对象
student stu1;
//对象数组
student stu2[3];
//new 一个对象
student* pStu = new student;
pStu->print();
delete pStu;
pStu = nullptr;
}
int main() {
CreateObject();
return 0;
}
-
对象访问(初始化)
-
通过提供公有接口传参的方式初始化数据 //通过公用接口初始化
void student::initStudentInfo(string name,int stuNum,int score) {
m_name = name;
m_stuNum = stuNum;
m_score = score;
} -
通过提供公有接口返回值的方式初始化数据 //通过提供公有接口返回值的方式初始化数据
string& student::initName() {
return m_name;
}
int& student::initStuNum(){
return m_stuNum;
}
int& student::initScore(){
return m_score;
} -
默认初始化 -
#include<iostream>
#include <string>
using namespace std;
class student {
int m_score = 0;//默认属性为私有属性
public:
void print();
void initStudentInfo(string name, int stuNum, int m_score);
string& initName();
int& initStuNum();
int& initScore();
protected:
//新标准,可以在类中给数据直接初始化
string m_name = "默认值";
int m_stuNum = 0;
private:
};
void student::print(){
cout << m_name << " " << m_stuNum <<" "<<m_score<< endl;
}
//通过公用接口初始化
void student::initStudentInfo(string name,int stuNum,int score) {
m_name = name;
m_stuNum = stuNum;
m_score = score;
}
//通过提供公有接口返回值的方式初始化数据
string& student::initName() {
return m_name;
}
int& student::initStuNum(){
return m_stuNum;
}
int& student::initScore(){
return m_score;
}
void CreateObject() {
//普通对象
student stu1;
//对象数组
student stu2[3];
//new 一个对象
student* pStu = new student;
pStu->print();
delete pStu;
pStu = nullptr;
}
void initObject() {
student* pStu = new student;
pStu->initStudentInfo("张三", 1001, 88);
pStu->print();
student* pStu1 = new student;
pStu1->initName() = "李四";
pStu1->initStuNum() = 1002;
pStu1->initScore() = 89;
pStu1->print();
delete pStu;
delete pStu1;
pStu = nullptr;
pStu1 = nullptr;
}
int main() {
//CreateObject();
initObject();
return 0;
}
-
面向对象编程 #include<iostream>
#include <string>
using namespace std;
struct Node {
string m_name;
int m_Num = 0;
Node* next = nullptr;
};
class List {
public:
Node* CreateNode(string name, int Num);
void insertHeadNode(string name, int Num);
void insertTailNode(string name, int Num);
void travelList();
protected:
Node* pList=nullptr;
private:
};
int main() {
List* list = new List;//创建对象 通过对象去访问成员函数来实现功能
list->insertHeadNode("张三", 1001);
list->insertHeadNode("李四", 1002);
list->insertHeadNode("王五", 1003);
list->insertHeadNode("周六", 1004);
list->insertTailNode("罗二", 1005);
list->travelList();
delete list;
return 0;
}
Node* List::CreateNode(string name, int Num) {
Node* pNewNode = new Node;
if (NULL == pNewNode) {
cout << "申请内存失败!" << endl;
return nullptr;
}
pNewNode->m_name = name;
pNewNode->m_Num = Num;
pNewNode->next = nullptr;
return pNewNode;
}
void List::insertHeadNode(string name, int Num) {
if (pList == nullptr)
{
pList = CreateNode(name, Num);
}
else
{
Node* pNewNode = CreateNode(name, Num);
pNewNode->next = pList;
pList = pNewNode;
}
}
void List::insertTailNode(string name, int Num) {
if (pList == nullptr)
{
pList = CreateNode(name, Num);
}
else
{
Node* pMove = pList;
while (pMove->next)
{
pMove = pMove->next;
}
pMove->next = CreateNode(name, Num);
}
}
void List::travelList() {
while (pList)
{
cout << pList->m_name << " " << pList->m_Num << endl;
pList = pList->next;
}
} ??????????????
|