c/c++第十六天

来源:互联网 发布:餐厅预约软件 编辑:程序博客网 时间:2024/06/15 09:47

链表

#include<stdio.h>struct stuff{    int num;    struct stuff *next;};void main(){    struct stuff *head=NULL;    struct stuff n1,n2,n3,n4,n5;    n1.num=1;    n2.num=2;    n3.num=3;    n4.num=4;    n5.num=5;    head=&n1;    n1.next=&n2;    n2.next=&n3;    n3.next=&n4;    n4.next=&n5;    n5.next=NULL;    printf("%d\t",head->num);    printf("%d\t",head->next->num);    printf("%d\t",head->next->next->num);    printf("%d\t",head->next->next->next->num);    printf("%d\n",head->next->next->next->next->num);    /*删除n4*/    n3.next=&n5;    n4.next=NULL;    /*添加n6在n3和n5之间*/    struct stuff n6;    n6.num=6;    n3.next=&n6;    n6.next=&n5;    /*添加n7在n5后*/    struct stuff n7;    n7.num=7;    n5.next=&n7;    n7.next=NULL;    while(head!=NULL){        printf("%d\t",head->num);        head=head->next;    }}

结果:这里写图片描述
结构体变量用 . 运算符来访问结构体的成员
指向结构体的指针用->来访问其指向的结构体的成员

类和对象

类定义格式class <类名>{    public:        <公有成员函数和数据成员说明>    private:        <私有成员函数和数据成员说明>}<各个成员函数的实现>

成员函数的实现采用下面的定义方式:

<类型标识符><类名>::<成员函数名><形参表>){    <函数体>}

::是作用域运算符

通过c++写了个小程序

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include<string>using namespace std;class Item{public:    char name[10];    int jg;private:};class ListInfo{public:    void init(Item a);private:};void ListInfo::init(Item a){    cout << "名字:" << a.name << endl;    cout <<"价格:"<< a.jg << endl;    cout << endl;}int main(){    ListInfo l1,l2,l3;    Item i1,i2,i3;    sprintf(i1.name, "裤子");    i1.jg = 2000;    sprintf(i2.name, "衣服");    i2.jg = 3000;    sprintf(i3.name, "鞋子");    i3.jg = 1000;    l1.init(i1);    l2.init(i2);     l3.init(i3);    getchar();    return 0;}

结果:这里写图片描述

0 0