链表,类(创建类,模拟商城)

链表:

存储特点:非连续非顺序的存储结构。通过指针实现链节的连接,实现实现跳跃型的连续存储。(个人理解)

#include<stdio.h>

void main()
{
    struct info 
    {
        int sum;
        struct info *next;
    }n1,n2,n3,n4,n5,n6,n7;      //定义结构体变量
    struct info *head = NULL;
    head = &n1;
    n1.sum = 1;
    n2.sum = 2;
    n3.sum = 3;
    n4.sum = 4;
    n5.sum = 5;
    n1.next = &n2;
    n2.next = &n3;
    n3.next = &n4;
    n4.next = &n5;
    n5.next = NULL;
    printf("\n%d",head->sum);
    printf("\n%d",head->next->sum);
    printf("\n%d",head->next->next->sum);
    printf("\n%d",head->next->next->next->sum);
    printf("\n%d\n",head->next->next->next->next->sum);   //这样好麻烦有没有?

    /*末尾增加链节n6*/        
    n6.sum = 6;
    n5.next = &n6;
    n6.next = NULL;
    //printf("\n%d",head->next->next->next->next->next->sum);   

    /*n3,n4中间插入链节n7*/   
    n7.sum = 7;
    n3.next = &n7;  
    n7.next = &n4;

    /*删除n4*/
    n7.next = &n5;
    n4.next = NULL;

    printf("\n最新效果:\n");

    /*打印链表所有元素*/
    while(head != NULL)
    {
            printf("\n%d",head->sum);
        head = head->next;      
    }
    head = &n1;    //头指针复位

    /*查询n5的值*/
    while(head != &n5)
    {
        head = head->next;
    }
    printf("\n\n%d",head->sum);
    head = &n1;    //头指针复位

    getchar();
}

类:

1,关键词:class
2,输入输出头文件应用格式:#include<iostream> #include<string>
3,打印格式:cout<<需要打印的内容<<需要打印的内容<

# include <iostream>     //应用输入输出头文件
# include <string>      //应用字符串头文件
using namespace std;   

class Item
{
public:
    string 类型;       //成员属性
    int 等级;
    string 职业;
    string 性别;        
    void 强化(int x);  //成员函数

private:
};

void Item::强化(int x)        //用::(域运算符)定义成员函数
{
    switch(x)
    {
        case 1: 
        cout<<"强化: *"<<endl;    //输出的函数cout<<需要打印的内容<<需要打印的内容<<endl
        break;
        case 2: 
        cout<<"强化: * *"<<endl;
        break;
        case 3: 
        cout<<"强化: * * *"<<endl;
        break;
    }
}

int main()
{
        Item item;            //用已经创建好的类Item创建一个对象item
        item.等级 = 1;         //给item对象的各个属性赋值
        item.类型 = "武器";
        item.职业 = "战士";
        item.性别 = "无限制";
        cout<<"等级: "<<item.等级<<endl;
        cout<<"类型:"<<item.类型<<endl;
        cout<<"职业:"<<item.职业<<endl;
        cout<<"性别:"<<item.性别<<endl;
        item.强化(3);
        getchar();
        return 0;
}

C++作用域运算符::的用法主要有2种应用方式:

一,类与类的成员之间

声明一个类A,类A里声明了一个成员函数void f(),但没有在类的声明里给出f的定义,那么在类外定义f时,就要写成void A::f(),表示这个f()函数是类A的成员函数。

##二,作用域
简单分为:全局作用域,局部作用域,语句作用域
作用域优先级:范围越小优先级越高
作用域运算符:”::”
如果希望在局部变量的作用域内使用同名的全局变量,可以在该变量前加上”::”.

模拟商城:

逻辑与步骤:
1,创建一个物品类Item
2,创建一个外框类Listinfo,public只留一个接口(一个以物品对象(Item a)为参数,负责打印物品属性的,成员函数init)
3,定义成员函数
4,在main函数中定义分别创建两个类的所属对象L1,n1。
5,给武平对象各属性赋值以后调用成员函数,格式为L1.init(n1);

# include <iostream>
# include <string>

using namespace std;

/*
*物品类
*/
class Item
{
public:
    string 道具名;
    int 价格;
    int 图片名;

private:
};

/*
*外框类
*/
class listinfo
{
public:
    void init(Item a);

private:
};

/*
*定义外框类的成员函数
*/
void listinfo::init(Item a)
{

    cout<<a.道具名<<endl;
    cout<<a.价格<<endl;
    cout<<a.图片名<<endl;
};

int main()
{
    listinfo L1;
    Item a;
    a.道具名 = "屠龙";
    a.价格 = 2000;
    a.图片名 = 111;
    L1.init(a);
    getchar();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值