C++类是对事物的一种抽象,万物都可以当做是类处理,类抽象的是什么?
对象可以说类的具体化(实例化) 美女(18,90kg)
class 类名
{
//默认为私有属性
//.....
public: //类外只能访问公有属性
//....公有属性
//公有函数--->类外的公有接口
protected:
//....保护属性
private:
//....私有属性
};
//保护和私有属性区别,暂时不需要知道,后续讲继承的时候会讲
//类中没有权限可言
class MM
{
public:
void printData()
{
cout << name << "\t" << age << "\t"<<num << endl;
}
void print();
protected:
private: //一般数据成员写:私有属性
string name;
int age;
int num;
};
//在类外实现类中的函数,必须要用类名限定(类名::函数名)
void MM::print()
{
cout << name << "\t" << age << "\t" << num << endl;
}
#include
using namespace std;
class MM
{
int num; //类中默认的是私有属性
public:
int age;
private:
string name;
};
struct Boy
{ //结构体中默认的公有属性
int num;
int age;
private:
string name;
};
//C语言是不能写空的结构体
//C++允许空结构体或者类
struct Empty
{
//占用内存不是0 是1(标记),写了数据这个标记就不存在
}m;
//作用:泛型编程做参数包解析的递归循环终止处理
int main()
{
struct Boy boy;
//boy.num = 1001;
//boy.age = 12;
MM mm;
mm.age = 19;
cout << sizeof(Empty) << endl;
cout << sizeof(Boy) << endl;
return 0;
}
#include
#include
using namespace std;
class MM
{
public:
void printData() //普通成员函数只是写在类中,不占对象内存
{
cout << name << "\t" << age << "\t"<<num << endl;
}
void print();
void initData(string mmName, int mmAge, int mmNum)
{
name = mmName;
age = mmAge;
num = mmNum;
}
string& getName() { return name; }
int& getAge() { return age; }
int& getNum() { return num; }
protected:
private: //一般数据成员写:私有属性
string name="默认值";
int age=0;
int num=0;
};
//在类外实现类中的函数,必须要用类名限定(类名::函数名)
void MM::print()
{
cout << name << "\t" << age << "\t" << num << endl;
}
int main()
{
MM mm; //创建对象
MM array[3];
MM* pMM;
pMM = &mm;
pMM = new MM; //new一个对象
//new一个对象过程
//1.创建一个匿名对象(没有名字的对象)
//2.把匿名对象的首地址赋值指针
pMM->print();
pMM->printData();
//pMM->name = "小芳"; 不可访问,类外不能访问public之外的所有属性
cout << "通过提供共有接口传参的方式初始化对象的数据" << endl;
mm.initData("小芳", 18, 1001);
mm.printData();
cout << "返回引用的去访问数据" << endl;
MM* p = new MM;
p->getName() = "MM";
p->getAge() = 28;
p->getNum() = 1004;
p->printData();
return 0;
}
#include
#include
#include
#include
using namespace std;
class MM
{
public:
void initData(string mmName, int mmAge, int mmNum)
{
name = mmName;
age = mmAge;
num = mmNum;
}
//不想让别人直接修改数据,不要返回引用,只能看不能操作
string getName() { return name; }
int getAge() { return age; }
int getNum() { return num; }
private:
string name;
int age;
int num;
};
void testObject()
{
MM mm;
mm.initData("小芳", 19, 1002);
cout << mm.getName() << "\t" << mm.getAge() << "\t" << mm.getNum() << endl;
//IMAGE img;
//img.getwidth();
//img.getheight();
}
void testObjectPoint()
{
MM* pMM = new MM;
pMM->initData("小丽", 29, 1004);
cout << pMM->getName() << "\t" << pMM->getAge() << "\t" << pMM->getNum() << endl;
}
void testArray()
{
MM array[3];
for (int i = 0; i < 3; i++)
{
string name = "name" + to_string(i);
array[i].initData(name,18+i,1002+i);
cout << (array + i)->getName() << "\t" << (array + i)->getAge() << "\t" << (array + i)->getNum() << endl;
}
}
int main()
{
//age = 123; 不能直接访问
testObject();
testObjectPoint();
testArray();
return 0;
}
含有指针的处理方案和C语言的结构体中含有指针的处理方案是一样的,相对于C语言来说多了权限问题
#include
#include
using namespace std;
class MM
{
public:
void initData(const char* mmName, int mmAge);
void initMM(const char* mmName, int mmAge);
void print()
{
cout << name << "\t" << age << endl;
}
char*& getName()
{
return name;
}
protected:
char* name;
int age;
};
//一般不采用这种方案处理,诟病很大
void MM::initData(const char* mmName, int mmAge)
{
name = (char *)mmName;
age = mmAge;
}
void MM::initMM(const char* mmName, int mmAge)
{
name = new char[strlen(mmName) + 1];
strcpy(name, mmName);
age = mmAge;
}
void printConst(const char* str) //传入常量和变量 传参const修饰
{
cout << str << endl;
}
int main()
{
MM* pMM = new MM;
pMM->initData("张三", 19);
pMM->print();
//strcpy(pMM->getName(), "ILoveyou"); // 百分百有问题,name没有指向一段可操作内存
char str[10] = "ILoveyou";
//C++const要求更为严格,尤其是字符串处理
printConst("ILoveyou");
printConst(str);
pMM->getName() =str;
pMM->print();
MM* p = new MM;
p->initMM("ILoveyou",20);
p->print();
strcpy(p->getName(), "IMiss");
p->print();
return 0;
}
只能包含自身指针不能包含自身对象
class Boy
{
public:
//Boy boy; 错误代码
Boy* pBoy; //指针正确
};
class Test
{
public:
void print(string info)
{
cout << info << endl;
}
};
void testFunc()
{
Test test;
//void (*Func)(string) = nullptr;
//Func = &Test::print; 错误写法,类型
//1.auto自动推断出类成员函数指针类型
auto Funcf = &Test::print; //没问题
(test.*Funcf)("测试函数"); //调用还是要学会
Test testb;
(testb.*Funcf)("测试函数2");
testb.print("直接调用");
//2.正规写一下正常写法
//不是简单的用*指针名替换函数,而是要加上类名限定
void (Test:: *Func)(string) = nullptr;
Func = &Test::print; //类中所有东西,无论怎么访问,必须类名限定
(test.*Func)("类成员函数指针访问成员函数");
}
class Student
{
public:
void print()
{
cout << name << "\t" << age << endl;
}
string& getName()
{
return name;
}
int& getAge()
{
return age;
}
protected:
string name;
int age;
};
void printData(Student student)
{
student.print();
}
//子函数修改实参,C语言传入实参地址,C++传引用
void modifyStudent(Student& stu)
{
stu.getName() = "Memory";
stu.getAge() = 28;
}
Student* createStu(string name, int age)
{
Student* p = new Student;
p->getName() = name;
p->getAge() = age;
return p;
}
void testStudent()
{
Student stu;
modifyStudent(stu);
printData(stu);
Student* p = createStu("C++", 19);
p->print();
Student temp = *p;
temp.print();
//除了赋值之外,其他运算不能直接做
//Student temp1 = temp + temp; //算术+条件
}
//C++的方式实现链表操作
//插入来一个
//遍历来一个
//删除来一个
//基础差的,链表的节点不需要用class
#include
using namespace std;
struct Node
{
int data;
struct Node* next;
};
class List
{
public:
void initList();
void insertData(int data);
void printData();
void deleteNode(int posData);
protected:
Node* headNode; //表示表头
int curSize; //当前节点个数
};
void List::initList()
{
headNode = new Node;
headNode->next = nullptr;
curSize = 0;
}
void List::insertData(int data)
{
//创建新节点
Node* newNode = new Node;
newNode->data = data;
newNode->next = nullptr;
//表头插入链接
newNode->next = headNode->next;
headNode->next = newNode;
curSize++;
}
void List::printData()
{
Node* pmove = headNode->next;
while (pmove != nullptr)
{
cout << pmove->data << " ";
pmove = pmove->next;
}
cout << endl;
}
void List::deleteNode(int posData)
{
Node* preNode = headNode;
Node* curPos = headNode->next;
while (curPos != nullptr && curPos->data != posData)
{
preNode = curPos;
curPos = preNode->next;
}
if (curPos == nullptr)
{
cout << "没有找到无法做指定位置删除..." << endl;
}
else
{
preNode->next = curPos->next;
delete curPos;
curPos = nullptr;
}
}
int main()
{
List* p = new List;
p->initList();
for (int i = 0; i < 3; i++)
{
p->insertData(i);
}
p->printData();
p->deleteNode(1);
p->printData();
return 0;
}