0000000000000000程序设计学习(c++)(课堂学习2)
1.用string对象处理字符串
当处理字符串时优先选择用string对象处理,用法比字符数组更简单且不易出错。

当用字符数组处理时:

#include<iostream>
#include<string.h>
using namespace std;
struct STUDENT
{int StudNo;
char StudName[33];
};
void DisplayStudent(STUDENT *pStud)
{cout<<"Student No :"<<pStud->StudNo<<endl;
cout<<"Student Name :"<<pStud->StudName<<endl;
cout<<"------------"<<endl;
}
int main()
{STUDENT Stud1,Stud2,Stud3;
Stud1.StudNo=1;
strcpy(Stud1.StudName,"zhang san");//字符串的赋值
Stud2.StudNo=2;
strcpy(Stud2.StudName,"Li si");
Stud3.StudNo=3;
strcpy(Stud3.StudName,"Wang wu");
DisplayStudent(&Stud1);
DisplayStudent(&Stud2);
DisplayStudent(&Stud3);
return 0;
}

将上述代码变换一下:

#include<iostream>
#include<string.h>
using namespace std;
struct STUDENT
{int StudNo;
char StudName[33];
void Display()
{cout<<"Student No :"<<StudNo<<endl;
cout<<"Student Name :"<<StudName<<endl;
cout<<"------------"<<endl;
}
};
int main()
{STUDENT Stud1,Stud2,Stud3;
Stud1.StudNo=1;
strcpy(Stud1.StudName,"zhang san");
Stud2.StudNo=2;
strcpy(Stud2.StudName,"Li si");
Stud3.StudNo=3;
strcpy(Stud3.StudName,"Wang wu");
Stud1.Display();
Stud2.Display();
Stud3.Display();
return 0;
}

当成员函数在类的定义以外时,格式如下:

返回值类型 类名::函数名()
{
语句组
}
例如,将上述代码变换:

#include<iostream>
#include<string>
using namespace std;
class CStudent
{public:
int StudNo;
char StudName[30];
void Display();
};
void CStudent::Display()
{cout<<"Student No :"<<StudNo<<endl;
cout<<"Student Name:"<<StudName<<endl;
cout<<"---------------"<<endl;
}
int main()
{CStudent Stud1,Stud2,Stud3;
Stud1.StudNo=1;
strcpy(Stud1.StudName,"Zhang san");
Stud2.StudNo=2;
strcpy(Stud2.StudName,"Li si");
Stud3.StudNo=3;
strcpy(Stud3.StudName,"Wang wu");
Stud1.Display();
Stud2.Display();
Stud3.Display();
return 0;
}

2.定义string对象
格式:
string 变量名;
以上述代码为例:

#include<iostream>
#include<string>
using namespace std;
class CStudent
{public:
int StudNo;
string StudName;
void Display();
};
void CStudent::Display()
{cout<<"Student No:"<<StudNo<<endl;
cout<<"Student Name:"<<StudName<<endl;
cout<<"------------------"<<endl;
}
int main()
{CStudent Stud[3];
CStudent *pStud;
int i;
Stud[0].StudNo=1;
Stud[0].StudName="Zhang san";
Stud[1].StudNo=2;
Stud[1].StudName="Li si";
Stud[2].StudNo=3;
Stud[2].StudName="Wang wu";
for(i=0;i<3;i++)
{pStud=&Stud[i];
pStud->Display();
}
return 0;
}

3.析构函数

#include<iostream>
using namespace std;
class CComplex
{private:
double real;
double imag;
public:
CComplex();
CComplex(double real,double imag);
CComplex(const CComplex &z);
~CComplex();double GetReal()const;
double Getimag()const;
void Display;
};
CComplex::CComplex()
{cout<<"Constructor CComplex::CComplex()!"<<endl;
real=0.0;
imag=0.0;
}
CComplex::CComplex(double real,double imag)
{cout<<"Constructor CComplex::CComplex(double real,double imag)!"<<endl;
this->real=real;
this->imag=imag;
}
CComplex::CComplex(const CComplex &z)
{cout<<"Copy  constructor CComplex::CComplex(const CComplex &z)!"<<endl;
real=z.real;
imag=z.imag;
}
CComplex::~CComplex()
{cout<<"Destructor CComplex::~CComplex()!"<<endl;
}
double CComplex::GetReal()const
{return real;
}
double CComplex::Getimag()const
{return imag;
}
void CComplex::Display()
{cout<<real<<"+"<<imag<<"i"<<endl;
}
CComplex Globle_z;
void DisplayComplex(const CComplex &z)
{cout<<z.GetReal()<<"+"<<z.Getimag()<<"i"<<endl;
}
int main()
{cout<<"---------------------"<<endl;
CComplex z1;
z1.Display();
cout<<"---------------"<<endl;
CComplex*pz1;
pz1=new CComplex;
pz1->Display();
delete pz1;
cout<<"-------------"<<endl;
CComplex z2(3.0,4.0);
z2.Display();
cout<<"-------------"<<endl;
CComplex *pz2;
pz2=new CComplex(3.0,4.0);
pz2->Display();
delete pzz2;
cout<<"-------------"<<endl;
CComplex z3(6.0,8.0);
DisplayComplex(z3);
cout<<"---------------"<<endl;
CComplex z4(z3);
z4.Display();
cout<<"--------------"<<endl;
return 0;
}

输出员工信息:

#include<iostream>
#include<string>
using namespace std;
class CEmployee
{private:
int ID;
string Name;
string Sex;
int Age;
double Salary;
public:
CEmployee();
~CEmployee();
int GetId();
void SetID(int ID);
string GetName();
void SetName(string Name);
string GetSex();
void SetSex(string Sex);
int GetAge();
void SetAge(int Age);
double GetSalary();
void SetSalary(double Salary);
void Display();
};
CEmployee::CEmployee()
{cout<<"Constructor CEmployee::CEmployee()!"<<endl;
ID=0;
Name="No Name";
Sex="Female";
Age=0;
Salary=0.0;
}
CEmployee::~CEmployee()
{cout<<"Destructor CEmployee::~CEmployee!"<<endl;
}
int CEmployee::GetID()
{return ID;
}
void CEmployee::SetID(int ID)
{if(ID>0)
{this->ID=ID;
}
}
string CEmployee::GetName()
{return Name;
}
void CEmployee::SetName(string Name)
{this->Name=Name;
}
string CEmployee::GetSex()
{return Sex;
}
void CEployee::SetSex(string Sex)
{if((Sex=="Female")||(Sex=="Male"))
{this->Sex=Sex;
}
}
int CEmployee::GetAge()
{return Age;
}
void CEmployee::SetAge(int Age)
{if((Age>=18)&&(Age<=60))
{this->Age=Age;
}
}
double CEmployee::GetSalary()
{return Salary;
}
void CEmployee::SetSalary(double Salary)
{if(Salary>0.0)
{this->Salary=Salary;
}
}
void CEmployee::Display()
{cout<<"-----------------"<<endl;
cout<<"Employee ID:"<<ID<<endl;
cout<<"Name :"<<Name<<endl;
cout<<"Sex  :"<<Sex<<endl;
cout<<"Age  :"<<Age<<endl;
cout<<"Salary :"<<Salary<<endl;
cout<<"======================="<<endl;
}
int main()
{CEmployee Employees[4];
Employees[0].SetID(1);
Employees[0].SetName("zhang shan");
Employee[0].SetSex("Male");
Employee[0].SetAge(25);
Employee[0].SetSalary(5000.0);
Employee[1].SetID(2);
Employee[1].SetName("Li si");
Employee[1].SetSex("Female");
Employee[1].SetAge(25);
Employee[1].SetSalary(5000.0);
Employee[2].SetID(3);
Employee[2].SetName("wang wu");
Employee[2].SetSex("Male");
Employee[2].SetAge(30);
Employee[2].SetSalary(8000.0);
for(int i=0;i<4;i++)
{Employees[i].Display();
}
cout<<"--------------------"<<endl;
for(int j=0;j<4;j++)
{if(Employees[j].GetID()!=0)
{cout<<"Employee   ID:"<<Employees[j].GetID()<<"\t";
cout<<"Name:"<<Employees[j].GetName()<<"\t";
cout<<"Sex:"<<Employees[j].GetSex()<<"\t";
cout<<"Age:"<<Employees[j].GetAge()<<"\t";
cout<<"Salary:"<<Employees[j].GetSalary()<<endl;
}
else
{cout<<"Invalid  Employee Information!"<<endl;
}
cout<<"------------------"<<endl;
}
return 0;
}

程序设计学习(c++)(课堂学习2)相关推荐

  1. 小甲鱼c语言课后作业_知识,就是力量——山财“学习小课堂”助你蓄力

    编者按 少年,你渴望 知识的力量 吗? 我们有山财最全的学习秘籍, 只待你的耐心翻阅与潜心修炼! 接下来,就让我们一起查收学习干货~ 内容摘要 本系列推送分为 [悦览篇][干货篇][招募篇] 三大模块 ...

  2. 20175317 《Java程序设计》第一周学习总结

    20175317 <Java程序设计>第一周学习总结 教材学习内容总结 本周学习了Java大致的开发步骤,完成了课件自带的习题. 学习了在windows与Linux系统下不同的编译方法,掌 ...

  3. 20155227 2016-2017-2 《Java程序设计》第九周学习总结

    20155227 2016-2017-2 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC简介 JDBC全名Java DataBase Connectivity,是java联 ...

  4. 自主互助四环节之计算机教案,自主互助学习型课堂的实施方案

    自主互助学习型课堂的实施方案 自主互助学习型课堂以它的高效.自主.学生的自学能力开发吸引了我们每位老师,为了更好地在我校实施自主互助学习型课堂,特制定以下实施方案. 一.预期目标. 通过本学期的&qu ...

  5. 20172318 2016-2017-2 《Java程序设计》第一周学习总结

    20172318 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 在教材中基本明白了计算机系统的运行方式,了解了对于高级语言是使用是掌握好编程的关键,掌握了一 ...

  6. 20155303 2016-2017-2 《Java程序设计》第二周学习总结

    20155303 2016-2017-2 <Java程序设计>第二周学习总结 教材学习内容总结 『注意』 "//"为单行批注符: "/*"与&quo ...

  7. 20165218 2017-2018-1 《Java程序设计》第四周学习总结

    20165218 2017-2018-1 <Java程序设计>第四周学习总结 教材学习内容总结 第五章 子类与继承 子类与父类 通过关键字extands定义子类 class 子类 exta ...

  8. 20155327《Java程序设计》第二周学习总结

    <Java程序设计>第二学习总结 教材学习内容总结 类型 byte(字节) shot(短整型) int(整型) long(长整型) float(浮点型) double(双精度) char( ...

  9. 20175208 《Java程序设计》第九周学习总结

    20175208 2018-2019-2 <Java程序设计>第九周学习总结 一.教材学习内容总结: 第11章 JDBC与MySQL数据库 MySQL数据库管理系统 MySQL数据库管理系 ...

最新文章

  1. python的0基础入门语法_学习小结(1)
  2. golang odbc mysql_go语言通过odbc操作Access数据库的方法
  3. MySQL性能优化(二)
  4. jenkins搭建_如何搭建移动端自动化测试平台?没错,就用Jenkins!
  5. Atom飞行手册翻译: 4.2 深入键表(keymap)
  6. 罗永浩吐槽clubhouse:玩了两天 没有一个房间能待上10分钟
  7. Google的银河英雄传说
  8. 基于vue的nuxt框架cnode社区服务端渲染
  9. leetcode python3 简单题14. Longest Common Prefix
  10. spring实现mqtt服务端_SpringBoot--实战开发--MQTT消息推送(六十)
  11. 39.django的ORM模型
  12. Apache详解(一)Internet和HTTP协议
  13. 【OPENCV】运行opencv时找不到Qt库
  14. Android OpenGL ES(十一):绘制一个20面体
  15. FOC项目知识点总结四 | 从 PWM 到 SVPWM
  16. 使用mui制作一个web2app类型的app
  17. 基于SpringBoot的小说网站
  18. 美洽在线客服系统UTM帮助文档
  19. 高职计算机应用与信息检索,高职计算机应用主题式教学模式实践
  20. matlab实验报告李琼指数函数,基于MATLAB的多元非线性回归模型

热门文章

  1. 书店图书销售管理系统SSM
  2. mx150 宏碁swift3_大众化的轻薄本-宏碁蜂鸟Swift 3评测报告
  3. 计算机工作流程新图,工作流程责任分工.doc
  4. 2018年EI收录中文期刊目录【转】
  5. ZOOM——C++软件开发岗(实习)——视频面试一面
  6. 第1-2课:算法设计常用思想之贪婪法
  7. TransCenter: Transformers with Dense Queries for Multiple-Object Tracking
  8. 基于软总线的实时组件调度技术研究
  9. 组织引入和实施PMO的策略方法
  10. 台式机通过网线与使用wifi的笔记本上网