171015 C++学习笔记-5
例 5-1 作用域实例// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。// 5_1.cpp#include "stdafx.h"#include<iostream>#include<string>#include <cmath>using namespace std;int i;//在全局命名空间中的全局变量n
·
例 5-1 作用域实例
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_1.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
int i; //在全局命名空间中的全局变量
namespace Ns {
int j; //在Ns命名空间中的全局变量
}
int main() {
i = 5; //为全局变量i赋值
Ns::j = 6; //为全局变量j赋值
{ //子块1
using namespace Ns;//使得当前块中可以直接引用Ns命名空间标识符
int i; //局部变量,局部作用域,与外部变量重名,对外不可见,内层对外屏蔽
i = 7;
cout << "i=" << i << endl; //输出7
cout << "j=" << j << endl; //输出6
}
cout << "i=" << i << endl; //输出5
return 0;
}
例 5-2 变量生存期与可见性
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_2.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
int i = 1;
void other() {
static int a = 2;
static int b;
int c = 10;
a += 2;
i += 32;
c += 5;
cout << "---OTHER---" << endl;
cout << "i: " << i << " a:" << a << " b:" << b << " c:" << c << endl;
b = a;
}
int main() {
static int a;
int b = -10;
int c = 0;
cout << "---MAIN---" << endl;
cout << "i: " << i << " a:" << a << " b:" << b << " c:" << c << endl;
c += 8;
other();
cout << "---MAIN---" << endl;
cout << "i: " << i << " a:" << a << " b:" << b << " c:" << c << endl;
i += 10;
other();
return 0;
}
例 5-3 具有静态和动态生存期对象的时钟程序
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_3.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class Clock //时钟类定义
{
public: //外部接口
Clock();
void setTime(int newH, int newM, int newS);//三个形参均具有函数原型作用域
void showTime();
private:
int hour, minute, second; //私有数据成员
};
Clock::Clock():hour(0), minute(0), second(0){} //构造函数
void Clock::setTime(int newH, int newM, int newS) //3个形参均具有函数局部作用域
{
hour = newH;
minute = newM;
second = newS;
}
void Clock::showTime() {
cout << hour << ":" << minute << ":" << second << endl;
}
Clock globClock; //声明对象globClock具有静态生存期,命名空间作用域
int main() { //主函数
cout << "First time output:" << endl;
//引用具有命名空间作用域的对象globClock
globClock.showTime(); //对象的成员函数具有类作用域
//显示0:0:0
globClock.setTime(8, 30, 30); //奖时间设置为8:30:30
Clock myClock(globClock); //申明具有块作用域的对象myClock
//调用复制构造函数,以globClock为初始值,为何此处没有[&对象名]???
cout << "Second time output:" << endl;
myClock.showTime(); //引用具有块作用域的对象myClock
return 0;
}
例 5-4 具有静态数据成员的Point类
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_4.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class Point { //Point类定义
public: //外部接口
Point(int x = 0, int y = 0) :x(x), y(y) { // 构造函数
//在构造体函数中对count累加,所有对象共同维护同一个count
count++; //构造函数 count+1
}
//复制构造函数
Point(Point &p) {
x = p.x;
y = p.y;
count++; //复制构造函数 count+1
}
//析构函数
~Point() { count--; } //当点消失时,析构函数令count-1
//成员函数-1
int getX() { return x; }
//成员函数-2
int getY() { return y; }
//成员函数-3
void showCount() {
cout << "\tObject count=" << count << endl; //输出静态数据成员
}
private: //私有数据成员
int x, y;
static int count; //静态数据成员申明,用于记录点的个数
};
int Point::count = 0; //静态数据成员定义和舒适化,实用类名限定
int main() { //主函数
Point a(4, 5); //定义对象a,其构造函数会使count+1
cout << "Point A: " << a.getX() << ", " << a.getY();
a.showCount(); //输出对象个数
Point b(a); //定义对象b,其构造函数会使count-1
cout << "Point B: " << b.getX() << ", " << b.getY();
b.showCount(); //输出对象个数
return 0;
}
例 5-5 具有静态数据和函数成员的Point类
与5-4相比,这里在类的定义中,将showCount改写成静态成员函数。于是在主函数中既可以使用类名,也可以使用对象名来调用showCount。
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_5.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class Point { //Point类定义
public: //外部接口
Point(int x = 0, int y = 0) :x(x), y(y) { // 构造函数
//在构造体函数中对count累加,所有对象共同维护同一个count
count++; //构造函数 count+1
}
//复制构造函数
Point(Point &p) {
x = p.x;
y = p.y;
count++; //复制构造函数 count+1
}
//析构函数
~Point() { count--; } //当点消失时,析构函数令count-1
//成员函数-1
int getX() { return x; }
//成员函数-2
int getY() { return y; }
//成员函数-3
static void showCount() { //静态函数成员
cout << "\tObject count=" << count << endl; //输出静态数据成员
}
private: //私有数据成员
int x, y;
static int count; //静态数据成员申明,用于记录点的个数
};
int Point::count = 0; //静态数据成员定义和舒适化,实用类名限定
int main() { //主函数
Point a(4, 5); //定义对象a,其构造函数会使count+1
cout << "Point A: " << a.getX() << ", " << a.getY();
Point::showCount(); //输出对象个数
Point b(a); //定义对象b,其构造函数会使count-1
cout << "Point B: " << b.getX() << ", " << b.getY();
Point::showCount(); //输出对象个数
return 0;
}
例 5-6 使用友元函数计算两点间的距离
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_6.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class Point { //Point类定义
public: //外部接口
Point(int x = 0, int y = 0) :x(x), y(y) {}
int getX() { return x; }
int getY() { return y; }
friend float dist(Point &p1, Point &p2);//友元函数申明
private: //私有数据成员
int x, y;
};
float dist(Point &p1, Point &p2) { //友元函数实现
double x = p1.x - p2.x; //通过对象访问私有数据成员
double y = p1.y - p2.y;
return static_cast<float>(sqrt(x*x + y*y));
}
int main() { //主函数
Point myp1(1, 1), myp2(4, 5); //定义point类的对象
cout << "The distance is: ";
cout << dist(myp1, myp2) << endl; //计算两点间的距离
return 0;
}
例 5-7 常用成员函数举例
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_7.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class R {
public:
R(int r1, int r2) :r1(r1), r2(r2) {} //内联 - 构造函数
void print();
void print() const;
private:
int r1, r2;
};
void R::print() {
cout << r1 << ":" << r2 << endl;
}
void R::print() const { //承诺不改变对象
cout << r1 << ";" << r2 << endl;
}
int main() { //主函数
R a(5, 4);
a.print(); //调用void print()
const R b(20, 52);
b.print(); //调用void print() const
return 0;
}
例 5-8 常数据成员举例
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_8.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class A {
public:
A(int i);
void print();
private:
//无static的为对象成员
const int a;
//有static的为类成员
static const int b; //静态常数据成员
};
const int A::b = 10; //静态常数据成员在类外说明和初始化,后面不可在变
A::A(int i) :a(i) {} //常数据成员只能通过初始化列表获得初值
void A::print() {
cout << a << ":" << b << endl;
}
int main() { //主函数
A a1(100), a2(0);
a1.print();
a2.print();
return 0;
}
例 5-9 常引用作形参
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_9.cpp
#include "stdafx.h"
#include<iostream>
#include<string>
#include <cmath>
using namespace std;
class Point { //Point 类定义
public: //外部接口
Point(int x = 0, int y = 0) :x(x), y(y) {}
int getX() { return x; }
int getY() { return y; }
friend float dist(const Point &p1, const Point &p2);
private: //私有数据成员
int x, y;
};
float dist(const Point &p1, const Point &p2) //常引用形参
{
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return static_cast<float>(sqrt(x*x + y*y));
}
int main() { //主函数
const Point myp1(1, 1), myp2(4, 5); //定义Point类
cout << "The distance is: ";
cout << dist(myp1, myp2); //计算两点间的距离
return 0;
}
例 5-10具有静态数据、函数成员的Point类,多文件组织 
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
// 5_10.cpp
#include "stdafx.h"
#include<iostream>
using namespace std;
//文件1, 类的定义,point.h
class Point { //类的定义
public: //外部接口
Point(int x = 0, int y = 0) :x(x), y(y) {}
Point(const Point &p);
~Point() { count--; }
int getX() const { return x; }
int getY() const { return y; }
static void showCount(); //静态函数成员
private: //私有函数成员
int x, y;
static int count; //静态数据成员
};
//文件2,类的实现,Point.cpp
#include "stdafx.h"
#include<iostream>
#include "point.h"
using namespace std;
int Point::count = 0; //使用类名初始化静态数据成员
Point::Point(const Point &p) :x(p.x), y(p.y) {//复制构造函数体
count++;
}
void Point::showCount() {
cout << " Object count= " << count << endl;
}
//文件3,主函数,5_10.cpp
#include "stdafx.h"
#include<iostream>
#include "point.h"
using namespace std;
int main()//主函数
{
Point a(4, 5); //定义对象a, 其构造函数会使count+1
cout << "Point A: " << a.getX() << ", " << a.getY();
Point::showCount(); //输出对象个数
Point b(a); //定义对象b, 其构造函数会使count+1
cout << "Point B: " << a.getX() << ", " << a.getY();
Point::showCount(); //输出对象个数
return 0;
}
例 5-11 个人银行账户管理程序
1-类的定义文件
// bankaccount.cpp : 定义控制台应用程序的入口点。
// account.h
#include "stdafx.h"
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
class SavingsAccount { //存储账户类
private:
int id; //账号ID
double balance; //余额
double rate; //存款的年利率
int lastDate; //上次变更余额的时间
double accumulation; //余额按日累加之和
static double total; //所有账户的总金额
//记录一笔账,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() const { return id; }
double getBalance() const { return balance; }
double getRate() const { return rate; }
static double getTotal() { return total; }
void deposit(int date, double amount); //存入现金
void withdraw(int date, double amount); //取出现金
//结算利息,每年1月1日调用一次该函数
void settle(int date);
//显示账户信息
void show()const;
};
#endif//__ACCOUNT_H_
2-类的实现文件
// account.cpp
#include "stdafx.h"
#include<iostream>
#include <cmath>
#include "account.h"
using namespace std;
double SavingsAccount::total = 0;
//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;
}
3-类的使用文件
//5_11.cpp
#include "stdafx.h"
#include "account.h"
#include <iostream>
using namespace std;
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;
cout << "Total: " << SavingsAccount::getTotal() << endl;
return 0;
}更多推荐



所有评论(0)