五、类、结构体、指针、引用
类可以将变量、数组和函数完美的打包在一起。
1.类与结构体
类的定义:
#include <iostream>
using namespace std;
class Person
{
private:
double score;
public:
string name;
int age;
void say()
{
cout<<"人会说话";
}
void set_score(double cj)
{
if(cj>=0 && cj<=100)
score=cj;
}
double get_score()
{
return score;
}
};
int main()
{
Person p1;
p1.set_score(92);
cout<<p1.get_score();
}
类中的变量和函数被统一称为类的成员变量。
private后面的内容是私有成员变量,在类的外部不能访问;public后面的内容是共有成员变量,在类的外部可以访问。
#include <iostream>
using namespace std;
const int N=1000010;
class Person
{
private:
int age,height;
double money;
string books[100];
public:
string name;
void say()
{
cout<<"I'm "name<<endl;
}
int set_age(int a)
{
age=a;
}
int get_age()
{
return age;
}
void add_money(double x)
{
money+=x;
}
}person_a,person_b,persons[100];
int main()
{
Person c;
c.name="qwe"; //正确!访问公有变量
c.age=18; //错误!访问私有变量
c.set_age(18); //正确!set_age()是公有成员变量
c.add_money(100);
c.say();
cout<<c.get_age()<<endl;
return 0;
}
类和结构体的作用是一样的。不同点在于类默认是private,结构体默认是public。
struct ren
{
private:
int age,height;
double money;
string books[100];
public:
string name;
void say()
{
cout<<"I'm"<<" ";
}
int sat_age(int a)
{
age=a;
}
int gat_age()
{
return age;
}
void add_money(double x)
{
money+=x;
}
}ren_a,ren_b,rens[100];
2、指针和引用
指针指向存放变量的值,因此我们可以通过指针来修改变量的值。
#include <iostream>
using namespace std;
int main()
{
int a=6;
int *q=&a;
*q+=1;
cout<<a;
return 0;
}
}
数组名是一种特殊的指针。指针可以做运算:
#include <iostream>
using namespace std;
int main()
{
int a[5]={1,2,3,4,5};
for(int q=0;q<5;q++)
{
cout<<*(a+q)<<endl;
}
return 0;
}
引用和指针相似,相当于给变量取了个别名。
#include <iostream>
using namespace std;
int main()
{
int a=10;
int &p=a;
p+=114504;
cout<<a;
return 0;
}
指针与结构体
#include <iostream>
using namespace std;
struct Node
{
int s;
Node*next;
};
int main()
{
Node *p1=new Node();
p1->s=1;
Node *p2=new Node();
p2->s=2;
p1->next=p2;
Node *p3=new Node();
p3->s=3;
p2->next=p3;
for(Node *head=p1;head;head=head->next)
{
cout<<head->s<<endl;
}
return 0;
}
链表
#include <iostream>
using namespace std;
struct Node
{
int s;
Node*next;
}*haed;
int main()
{
for(int i=1;i<=3;i++)
{
Node *p=new Node();
p->s=i;
p->next=haed;
haed=p;
}
for(Node *p=haed;p;p=p->next)
{
cout<<p->s<<endl;
}
return 0;
}
删除链表的节点
void Delete(int n)
{
Node*t1=haed;
if(n==1)
{
haed=t1->next;
delete t1;
return;
}
for(int i=1;i<n-1;i++)
{
t1=t1->next;
}
Node*t2=t1->next;
t1->next=t2->next;
delete t2;
}
链表的插入
void Insert(int q,int w)
{
Node*t1=new Node();
t1->s=q;
if(w==1)
{
t1->next=haed;
haed=t1;
return;
}
Node *t2=haed;
for(int i=1;i<w-1;i++)
{
t2=t2->next;
}
t1->next=t2->next;
t2->next=t1;
}
链表的反转
void Fz()
{
Node *prev=NULL;
Node *curr=haed;
Node *next;
while(curr!=NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
haed=prev;
}
双向链表删除
void Delete(int n)
{
Node *t1=haed;
if(n==1)
{
haed=t1->next;
haed->syg=NULL;
delete t1;
return;
}
for(int i=1;i<n-1;i++)
{
t1=t1->next;
}
Node *t2=t1->next;
if(t2->next!=NULL)
{
t1->next=t2->next;
t1->next->syg=t1;
}
else
{
t1->next=NULL;
}
delete t2;
return;
}
插入双向链表
void Insert(int s,int n)
{
Node *t1=new Node();
t1->s=s;
if(n==1)
{
t1->next=haed;
haed->syg=t1;
t1->syg=NULL;
return;
}
Node *t2=haed;
for(int i=1;i<n-1;i++)
{
t2=t2->next;
}
if(t2->next!=NULL)
{
t1->next=t2->next;
t1->syg=t2;
t2->next=t1;
}
else
{
t2->next=t1;
t1->syg=t2;
}
return;
}
浙公网安备 33010602011771号