、C++类和对象
类和对象的基本概念
类的定义
#include"iostream"
using namespace std;
struct MM {
public:
void print()
{
cout << "Im class member function" << endl;
}
protected:
string name;
private:
int age;
};
- 什么是类,类是对事物的特性和行为进行抽象出来的概念,不管什么样的人,ta就属于人类;
还有其他:书:不管大小形状颜色。。
女朋友:没有女朋友之前这是在你脑海中的概念,当你有了女朋友 具体的她就是类的对象。- 类是有两部分组成: 属性 行为
比如女朋友类:属性 (长相)行为(她会跟你聊天撒娇发脾气) - 属性: 事物的特征—>数据类型描述
- 行为: 事物的操作—>函数描述
- 类是有两部分组成: 属性 行为
- 什么是对象: 类的具体化,类的实例化.
你的具体的女朋友 - 类的特点: 封装,继承/派生,多态
对三个类再抽象就是 哺乳动物类或者动物。
继承:电子书就是继承了纸质书的特性和一些行为,但它有自己的独有特性行为。
********多态 不了解?????
对象创建
MM mm;// MM 类的对象 mm
普通对象
#include"iostream"
using namespace std;
struct MM {
public:
void print()
{
cout << "Im class member function" << endl;
cout << name << "\t" << age << endl;
}
void initdata(string nname, int aage)
{
name = nname;
age = aage;
}
protected:
string name="Alica";
private:
int age=18;
};
int main()
{
MM mm;// MM 类的对象 mm
mm.print();
while(1);
return 0;
}
对象数组
new 一个对象
成员访问(初始化)
通过提供 公有接口传参的方式初始化数据
通过提供 公有接口返回值的方式初始化数据
#include"iostream"
using namespace std;
struct MM {
public:
void print()
{
cout << "Im class member function" << endl;
cout << name << "\t" << age << endl;
}
void initdata(string nname, int aage)
{
name = nname;
age = aage;
}
protected:
string name="Alica";
private:
int age=18;
};
int main()
{
MM mm;// MM 类的对象 mm
mm.print();
MM mmarry[4];//一般很少用对象数组
for (int i = 0; i < 4; i++)
{
mmarry[i].initdata("Alica", 18+i);
mmarry[i].print();
}
// new a object
MM* p = new MM;
p->initdata("Nickshira", 20);
p->print();
delete p;
while(1);
return 0;
}
面向对象的编程方式
面向对象的编程是指 一切都是类,无论是某个物体还是方法,都是类,通过抽象成类,然后通过创建对应对象;然后对象之间的关系来实现 目的。
例如:你想搭讪女孩子<你和她都是人类的对象,你跟她搭话 是聊天说话类的对象,只是这些对象的属性和行为不一样而已。> 记住:一切都是类加粗样式的对象,都可以抽象再创建。
C语言方式 封装:
#include"iostream"
using namespace std;
//struct MM {
//public:
// void print()
// {
// cout << "Im class member function" << endl;
// cout << name << "\t" << age << endl;
// }
// void initdata(string nname, int aage)
// {
// name = nname;
// age = aage;
// }
//
//protected:
// string name = "Alica";
//private:
// int age = 18;
//};
//MM mm;// MM 类的对象 mm
//mm.print();
//MM mmarry[4];//一般很少用对象数组
//for (int i = 0; i < 4; i++)
//{
// mmarry[i].initdata("Alica", 18 + i);
// mmarry[i].print();
//}
new a object
//MM* p = new MM;
//p->initdata("Nickshira", 20);
//p->print();
//delete p;
struct Node {
int data;
Node* next;
};
Node* creatlist()
{
cout << "我是creatlist" << endl;
Node* headnode = new Node;
headnode->next = nullptr;
return headnode;
}
Node* /*??*/creatnewnode(int data)
{
cout << "我是creatnewnode" << endl;
Node* newnode = new Node;
newnode->data = data;
newnode->next = nullptr;
return newnode;//?
}
void inseartnode(Node* headnode, int data)
{
cout << "我是 inseartnode" << endl;
Node* newnode = new Node;
newnode->data = data;
newnode->next = headnode->next;
headnode->next = newnode;
}
void printlist(Node* headnode)
{
Node* pnode = headnode;
while (pnode!=nullptr)
{
cout <<"printlist=" << pnode->data <<endl;
pnode = pnode->next;
}
}
void testlist()
{
Node* list = creatlist();
inseartnode(list, 20);
inseartnode(list, 22);
printlist(list);
}
int main()
{
testlist();
while (1);
return 0;
}
那个 负数我还没搞明白
用结构体来写:
#include"iostream"
using namespace std;
struct Node {
int data;
Node* next;
};
class list {
public:
void createlist()
{
headnode = new Node;
headnode->next = nullptr;// 不需要返回
}
void inseartdata(int data)
{
Node* newnode = new Node;
newnode->data = data;
newnode = nullptr; //可以不赋值吗?
newnode->next = headnode->next;
headnode->next = newnode;
}
void printlist()
{
Node* pmove = headnode->next;
while (pmove->next != nullptr)
{
cout << pmove->data << endl;
pmove = pmove->next;
}
}
protected:
private:
Node* headnode;//一个指针表示表头
};
void texstlist()
{
list* List = new list; //C++直接创建对象,才能具体的对象来 实现
List->inseartdata(10);
List->inseartdata(20);
List->printlist();
}
int main()
{
//testlist();
texstlist();//结构体来写
while (1);
return 0;
}
总结
面向对象编程时,定义完类做任何行为之前必须创建对象;
然后创建链表时,用结构体可以避免class的权限处理的麻烦。