大学C++基础――结构体与类课件

上传人:我*** 文档编号:145744161 上传时间:2020-09-23 格式:PPT 页数:30 大小:121.50KB
返回 下载 相关 举报
大学C++基础――结构体与类课件_第1页
第1页 / 共30页
大学C++基础――结构体与类课件_第2页
第2页 / 共30页
大学C++基础――结构体与类课件_第3页
第3页 / 共30页
大学C++基础――结构体与类课件_第4页
第4页 / 共30页
大学C++基础――结构体与类课件_第5页
第5页 / 共30页
点击查看更多>>
资源描述

《大学C++基础――结构体与类课件》由会员分享,可在线阅读,更多相关《大学C++基础――结构体与类课件(30页珍藏版)》请在金锄头文库上搜索。

1、程序设计(C+)(00864094),第8讲 结构体与类 20112012学年秋季学期 (CPP111),C 结构体,一般结构体,结构体定义、引用、输入输出、赋值 结构体与内存 结构体数组、结构体指针 结构体指针做为函数参数 结构体做为函数返回类型,结构体的提出,单一对象有许多相关信息,这些信息彼此相关,物理上(文件/内存)连续,要求能用统一、简单方法引用整体或各子信息。 将相关各子信息进行打包,打包的结果就是结构体。这是一种自定义的、新的数据类型。用“结构体名.分量名”的方法可对子信息进行引用。,结构体类型的定义,例:学生信息 学号num 姓名name 成绩 score; 定义了新的数据类型

2、 struct student,结构体类型定义 struct student int num; char name20; float score; ;,结构体变量的定义和引用,struct student st1; cin st1.num st1.name st1.score; cout st1.num , st1.name , st1.score endl;,结构体变量的内存分配,结构变量的地址 ,结构体数组,3个学生的信息 struct student stu3; 结构体数组的初始化 struct student stu3= 101, wang, 78.5, 第2个, 第3个 ; 结构体数

3、组的内存分配 各学生依次分配内存 结构体数组的引用、输入、输出 stui.num stui.name stui.namej,结构体指针,指向结构体变量的指针 struct student *p; 指针的初始化 p = 通过指针引用结构体 (*p).nump-num (*p).namep-name,结构体与函数:通过指针传递,参数传递:单个结构体变量 void printstudent( struct student *p ); 参数传递:结构体数组 float averscore(struct student st, int n ); 或 float averscore( struct stu

4、dent *p, int n ); 函数返回:结构体变量 struct student *findstudent( struct student st, int n, int num );,举例1,输入10个学生的信息,计算平均分。再输入一个学号,查找并输出该学号学生的信息。 struct student int num; char name20; float score; ;,输入与输出,void prt(struct student st ,int n) int i; for (i=0;i sti.num sti.name sti.score; ,求平均成绩,float averscore

5、(struct student st, int n) float sum; int i; sum = 0; for( i=0; i n; i+ ) sum += sti.score; return sum/n; ,查找学生,struct student *findstudent(struct student st, int n, int num) int i; for ( i = 0; i n; i+ ) if ( sti.num = num ) return ,例1主程序,main() struct student stu10, *p; int num; sca ( stu, 10 ); c

6、out num; p = findstudent( stu, 10, num ); if ( p != NULL ) prt( p,1 ); ,C+ 类与对象,类的定义,class 类名 private: 私有成员 public: 公共成员 protected: 保护成员 ;,类的成员 成员数据 成员函数 公共成员 外部可以访问 私有成员 内部可以访问 保护成员 内部或继承者可以访问,C+字符串string,#include char a80;/C字符串 string s1;/C+字符串对象 string s2(Hello);/定义并初始 string s3(a);/定义并初始 cin s1;

7、/输入 cout =s2/判断字符串s1是否在s2之后(字典顺序),C+字符串流对象,#include 输出() string s2(111 22.2); istringstream iss(s2); iss n x;,类设计示例,class myExp public: double mVal; /公式的值 string mStr; /公式表达式 int mPri; /计算优先级,0-原子,1-乘除,2-加减 myExp(); /构造函数,基本 myExp(int n); /构造函数,重载 myExp() /析构函数 myExp operator + (const myExp,构造函数,建立对

8、象时,myExp:myExp() mVal = 0; mStr = ; mPri = 0; myExp:myExp(int n) ostringstream ostr; /定义输出流对象 ostr n; /使用流对象输出 mVal = n; /初始数值 mStr = ostr.str(); /流对应字符串 mPri = 0; /优先级,原子 ,加运算+,myExp myExp:operator + (const myExp /返回运算结果 ,减运算-,myExp myExp:operator - (const myExp /返回运算结果 ,乘运算*,myExp myExp:operator *

9、 (const myExp /返回运算结果 ,除运算/,myExp myExp:operator / (const myExp /返回运算结果 ,使用示例(myExp.cpp),void myExpTest(void) myExp t; t = myExp(1) + myExp(1)/(myExp(1)*myExp(2) + myExp(1)/(myExp(1)*myExp(2)*myExp(3); cout t.mStr = t.mVal endl; ,关于容器,向量对象,#include vector lst;/多个公式的列表 lst.clear();/列表清空 lst.push_back

10、(x);/将公式x添加到列表末尾 lst.pop_back();/删除列表末尾的公式 lst.size();/返回列表中的公式数 lsti/取出列表中下标i公式 vector found;/多个字符串的列表,24点求解示例(x24.cpp),#include #include #include #include using namespace std; / class myExp public: double mVal; /公式的值 string mStr; /公式表达式 int mPri; /计算优先级,0-原子,1-乘除,2-加减 myExp(); /构造函数,基本 myExp(int n); /构造函数,重载 myExp() /析构函数 myExp operator + (const myExp ,

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > PPT模板库 > PPT素材/模板

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号