0
点赞
收藏
分享

微信扫一扫

171014 C++学习笔记-4


例 4-1 时钟类的完整程序

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

//Part One:定义类
class Clock {
public:
void setTime(int newH = 0, int newM = 0, int newS = 0);
void showTime();
private:
int hour, minute, second;
};

//Part Two:成员函数的实现
void Clock::setTime(int newH, int newM, int newS) {
hour = newH;
minute = newM;
second = newS;
}

inline void Clock::showTime() {
cout << hour << ":" << minute << ":" << second << endl;
}

//Part Three:主函数
int main() {
Clock myClock;
cout << "First time set and output:" << endl;
myClock.setTime();
myClock.showTime();
cout << "Second time set and output:" << endl;
myClock.setTime(8, 30, 30);
myClock.showTime();
return 0;
}

例 4-2 Point类的完整程序

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Point {
public:
Point(int xx = 0, int yy = 0) { //内联函数
x = xx;
y = yy;
}
Point(Point &p); // 复制构造函数
int getX() {
return x;
}
int getY() {
return y;
}
private:
int x, y;
};

//成员函数的实现,复制构造函数
Point::Point(Point &p) {
x = p.x;
y = p.y;
cout << "Calling the copy constructor" << endl;
}

// 形参为Poin类对象的函数
void fun1(Point p) {
cout << p.getX() << endl;
}
// 返回值为Point类对象的函数
Point fun2() {
Point a(1, 2);
return a;
}
int main()
{
Point a(4, 5); //第一个对象a
Point b = a; //情况一,用a初始化b。第一次调用复制构造函数
cout << b.getX() << endl;
fun1(b); //情况二,对象b作为fun1的实参。第二次调用复制函数
b = fun2(); //情况三,函数的返回值是类对象,函数返回时,调用复制构造函数
cout << b.getX() << endl;
return 0;
}

例 4-3 游泳池改造预算,Circle类

// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

const float PI = 3.141593; //给出PI的值
const float FENCE_PRICE = 35; //栅栏的单价
const float CONCRETE_PRICE = 20; //过道水泥单价

class Circle { //声明定义类Circle及其数据和方法
public: //外部接口
Circle(float r); //构造函数
float circumference(); //计算圆的周长
float area(); //计算圆的面积
private: //私有数据成员
float radius; //圆半径
};
Circle::Circle(float r) {
radius = r;
}
// 等价于:Circle::Circle(float r):radius(r){} --->列表表达式,效率更高

//计算圆的周长
float Circle::circumference() {
return 2 * PI*radius;
}

//计算圆环的面积
float Circle::area() {
return PI*radius*radius;
}

int main()
{
float radius;
cout << "Enter the radius of the pool:";//提示用户输入半径
cin >> radius;
Circle pool(radius); //游泳池边界对象
Circle poolRim(radius + 3); //栅栏对象
//计算栅栏造价并输出
float fenseCost = poolRim.circumference()*FENCE_PRICE;
cout << "Fencing Cost is $ " << fenseCost << endl;

//计算过道造价并输出
float concreCost = (poolRim.area() - pool.area())*CONCRETE_PRICE;
cout << "Concrete Cost is $ " << concreCost << endl;

return 0;
}

例 4-4 类的组合,线段(Line)类

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<cmath>
using namespace std;

class Point { //Point类的定义
public:
//构造函数,隐式申明
Point(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
//复制构造函数
Point(Point &p);
//内联成员函数,隐式声明;
int getX() { return x; }
int getY() { return y; }
private:
int x, y;
};

//复制构造函数的实现
Point::Point(Point &p) {
x = p.x;
y = p.y;
cout << "Calling the copy constructor of Point" << endl;
}

//类的组合
class Line { //Line类的定义
public: //外部接口
Line(Point xp1, Point xp2); //构造函数,形参为Point类
Line(Line &l); //复制构造函数,内联成员函数
double getLen() { return len; }
private:
Point p1, p2; //Point类的对象p1,p2
double len;
};

//组合类的构造函数
Line::Line(Point xp1, Point xp2) : p1(xp1), p2(xp2) //初始化列表,奖形参xp1,xp2赋予p1,p2
{
cout << "Calling constructor of Line" << endl;
double x = static_cast<double>(p1.getX() - p2.getX());
double y = static_cast<double>(p1.getY() - p2.getY());
len = sqrt(x*x + y*y); //计算得到len,len仅能被Line类中的共有函数getLen()访问
}

//组合类的复制构造函数
Line::Line(Line &l) :p1(l.p1), p2(l.p2) //l为对象名
{
cout << "Calling the copy constructor of Line" << endl;
len = l.len;
}

int main() {
Point myp1(1, 1), myp2(4, 5); //建立Point类的对象
Line line(myp1, myp2); //建立Line类的对象
Line line2(line); //利用复制构造函数建立一个新对象
cout << "The length of the line is: ";
cout << line.getLen() << endl;
cout << "The length of the line2 is: ";
cout << line2.getLen() << endl;
return 0;
}

例 4-7 用结构体表示学生的基本信息

有时程序中需要定义一些数据类型,它们并没有什么操作,定义它们的目的只是将一些不同类型的数据组合成一个整体,从而方便地保存数据,这样的类型不妨定义为结构体

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
//#include<iomanip> 流控制符:1.设置显示宽度 2.设置左右对齐 3.设置浮点数精度
#include<string>
using namespace std;

struct Student {
int num;
string name;
char sex;
int age;
};


int main() {
Student stu = { 97001,"Lin Lin",'F',19 };
cout << "Num: " << stu.num << endl;
cout << "Name: " << stu.name << endl;
cout << "Sex: " << stu.sex << endl;
cout << "Age: " << stu.age << endl;
return 0;
}

例 4-8 使用联合体保存成绩信息,并且输出

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

class ExamInfo {
public:
//3种构造函数,分别用登记、是否通过和百分制来初始化
ExamInfo(string name, char grade)
:name(name), mode(GRADE), grade(grade) {}
ExamInfo(string name, bool pass)
:name(name), mode(PASS), pass(pass) {}
ExamInfo(string name, int percent)
:name(name), mode(PERCENTAGE), percent(percent) {}
void show();

private:
string name; //课程名称
enum {
GRADE,
PASS,
PERCENTAGE
}mode; //采用何种计分方式,此处为何是这样的写法???
union {
char grade; //等级制的成绩
bool pass; //是否通过
int percent; //百分制的成绩
};
};

void ExamInfo::show() //成员函数
{
cout << name<<": ";

switch (mode) {
case GRADE: cout << grade;
break;
case PASS:
cout << (pass ? "PASS" : "FAIL");
break;
case PERCENTAGE:
cout << percent;
break;
}
cout << endl;
}

int main() {
ExamInfo course1("English", 'B');
ExamInfo course2("Calculus", true);
ExamInfo course3("C++Programming", 85);
course1.show();
course2.show();
course3.show();
return 0;
}

例 4-9 个人银行账户管理程序

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;

class SavingsAccount { //存储账户类
private:
int id; //账号ID
double balance; //余额
double rate; //存款的年利率
int lastDate; //上次变更余额的时间
double accumulation; //余额按日累加之和

//记录一笔账,date为日期,amount为金额,desc为说明
void record(int date, double amount);
//获得指定日期为止的存款金额按日累加值
double accumulate(int date) const{
return accumulation + balance*(date - lastDate);
}
public:
//构造函数
SavingsAccount(int date, int id, double rate);
int getId() { return id; }
double getBalance() { return balance; }
double getRate() { return rate; }
void deposit(int date, double amount); //存入现金
void withdraw(int date, double amount); //取出现金
//结算利息,每年1月1日调用一次该函数
void settle(int date);
//显示账户信息
void show();
};

//SavingsAccount 类相关成员函数的实现
//-1 构造函数,初始化对象
SavingsAccount::SavingsAccount(int date, int id, double rate)
:id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
cout << date << "\t#" << id << " is created" << endl;
}

//-2
void SavingsAccount::record(int date, double amount) {
accumulation = accumulate(date);
lastDate = date;
amount = floor(amount * 100 + 0.5) / 100;
balance += amount;
cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
}

//-3
void SavingsAccount::deposit(int date, double amount) {
record(date, amount); }


//-4
void SavingsAccount::withdraw(int date, double amount) {
if (amount > getBalance())
cout << "Error: not enough money" << endl;
else
record(date, -amount);
}
//-5
void SavingsAccount::settle(int date) {
double interest = accumulate(date)*rate / 365;
if (interest != 0)
record(date, interest);
accumulation = 0;
}
//-6
void SavingsAccount::show() {
cout << "#" << id << "\tBalance: " << balance;
}

int main() {
//建立几个账号
SavingsAccount sa0(1, 21325302, 0.015);
SavingsAccount sa1(1, 58320212, 0.015);
//几笔账目
sa0.deposit(5, 5000);
sa1.deposit(25, 10000);
sa0.deposit(45, 5500);
sa1.withdraw(60, 4000);
//开户后第90天到了银行的计息日,结算所有账户的年息
sa0.settle(90);
sa1.settle(90);
//输出各个账户的信息
sa0.show(); cout << endl;
sa1.show(); cout << endl;
return 0;
}


举报

相关推荐

0 条评论