结构体
include<iostream>
include<string>
using namespace std;
struct cartype
{
string maker;
int year;
float price;
string wheel[4];
enginetype enigne;
**//在声明cartype后,enigne才是一个对象,否则它就只是定义的一部分**
};
struct cartype
{
string maker;
int year;
float price;
};
void getyourcar(cartype & car);
int main()
{
cartype mycar, yourcar;
mycar.maker = "mercedes";
mycar.year = 2005;
mycar.price = 45567.75;
getyourcar(yourcar);
cout << "your car is a:" << yourcar.maker << endl;
cout << "i'll offer " << yourcar.price << " for your car." << endl;
return 0;
}
void getyourcar(cartype & car)
{
cout << "enter your maker:" << endl;
cin >> car.maker;
cout << "enter the year:" << endl;
cin >> car.year;
cout << "enter the price:" << endl;
cin >> car.price;
}
struct enginetype
{
int numcylinders;
float numliters;
string countrymade;
};
> 若enginetype生成一个myengine对象,并且试图将myengine赋值给yourcar,编译器会报错
>
> 但是可以采用如下代码:
>
> yourcar.engine=myengine
>
> 但是可以采用如下代码:
>
> yourcar.engine=myengine
结构体总结
1. 结构的对象可以赋值给同一结构的另外一个对象
例如:mycar=yourcar
此时,yourcar中的每个成员赋值给mycar中相对应的成员,wheel数组中的值也同样都赋值成功
2. 但是数组单独赋值给另外一个数组时,编译器会报错。
类与结构体区别:
1. 结构体以关键字struct打头,类以关键字class打头。
2. 若不在结构体和类中指定共有部分和私有部分,则结构体中任何内容都指定为公用的,而类中的都指定为私有的。
3. 由类生成的对象除了包含常规的数据成员外,通常还包含函数成员。
4. 在正确编写的类中,程序只能访问由类生成的对象的函数。
2. 若不在结构体和类中指定共有部分和私有部分,则结构体中任何内容都指定为公用的,而类中的都指定为私有的。
3. 由类生成的对象除了包含常规的数据成员外,通常还包含函数成员。
4. 在正确编写的类中,程序只能访问由类生成的对象的函数。
类
将函数定义放在类定义外
每个类应该使用2个文件,即3个类应使用7个文件(其中包括一个主程序文件)
类的两个文件分别为类说明文件(class specificationfile)和类实现文件(class implementation file)
类的两个文件分别为类说明文件(class specificationfile)和类实现文件(class implementation file)
类说明文件:仅包含类的定义(包含函数的声明和函数的原型)
类实现文件:包含类定义中函数原型的所有函数定义
类说明文件(Checkbook.h)
class Checkbook
{
public:
void setBalance(float amount);
//return false if amount is greater than balance;
//otherwise return true
bool writeCheck(float amount);
void deposit(float amount);
float getBalance();
float getLastCheck();
float getLastDeposit();
private:
float balance;
float lastCheck;
float lastDeposit;
};
balance变量为什么要位于类定义中,而不声明在要开始使用它的setBalance函数中??
- 在类定义中声明变量存在优势(不适用于局部变量):
对象总是可以记住类定义中的变量的值,即使对象的代码已不在运行。因此,当从setBalance函数返回时对象之外的程序又开始执行时,对象仍保留有balance变量的值。
- 类的函数内部声明变量的优势:
当需要临时使用变量时可以这样做(如for循环中的i)
当需要临时使用变量时可以这样做(如for循环中的i)
类实现文件(Checkbook.c)
//返回类型为void:因为在函数返回到程序后,对象不需要再与程序进行通信
void Checkbook::setBalance(float amount)
{
balance = amount;
}
bool Checkbook::writeCheck(float amount)
{
if (amount > balance)
return false;
balance -= amount;
lastCheck = amount;
return true;
}
void Checkbook::deposit(float amount)
{
balance += amount;
lastDeposit = amount;
}
float Checkbook::getBalance()
{
return balance;
}
float Checkbook::getLastCheck()
{
return lastCheck;
}
float Checkbook::getLastDeposit()
{
return lastDeposit;
}
test.c文件
Checkbook对象与主程序是分离的。
主程序只知道类的函数、必须传送给函数的内容、函数可以完成什么任务以及可以从函数接收什么内容等;不知道类定义中声明的变量、以及函数的内容或者函数中声明的变量
int menu();
const int CHECK = 1, DEPOSIT = 2, BALANCE = 3, QUIT = 4;
int main()
{
Checkbook cb;
float balance, amount;//此处声明的balance与类定义中声明的balance完全不同,毫不相干
int choice;
cout << "Enter the inital balance:$\n" << endl;
cin >> balance;
//对象的名称必须添加
cb.setBalance(balance);
cout << fixed << showpoint << setprecision(2);
//通过choice选择所要进行的操作
choice = menu();
while (choice != QUIT)
{
//填写支票
if (choice == CHECK)
{
cout << "Enter check amount:$\n" << endl;
cin >> amount;
if (cb.writeCheck(amount))
cout << "Check accepted." << endl;
else
{
cout << "Your balance is not high ";
cout << "enough for that check." << endl;
}
}
//存储
else if (choice == DEPOSIT)
{
cout << "Enter deposit amount:$";
cin >> amount;
cb.deposit(amount);
cout << "Deposit accepted." << endl;
}
//查询余额
else
{
//must be a balance request
amount = cb.getBalance();
cout << "Your balance is:$" << amount << endl;
}
//主程序又显示一个菜单项,while循环持续运行
choice = menu();
}
return 0;
}
//通过菜单选择所要进行的操作
int menu()
{
int choice;
cout << endl;
cout << "1 Write a check" << endl;//填写支票
cout << "2 Make a Deposit" << endl;//存款
cout << "3 Get the balance" << endl;//查看余额
cout << "4 Quit" << endl << endl;//退出
cout << "Enter a number between 1 and 4:";
cin >> choice;
return choice;
}
将函数定义放在类定义中
仅当函数定义中没有几行代码时才可以使用。
类总结
1. 一旦声明了一个类的对象,就会为该对象生成一组数据成员(对应于类),而且主程序也就可以访问该对象内的公用函数了。
2. 程序通过与对象进行通信而使用对象。
通信由函数执行。程序通过调用对象的一个成员函数与对象进行通信;对象也可以通过程序所调用的函数的返回值与程序通信。
3. 程序不能同时使用它的所有对象。
2. 程序通过与对象进行通信而使用对象。
通信由函数执行。程序通过调用对象的一个成员函数与对象进行通信;对象也可以通过程序所调用的函数的返回值与程序通信。
3. 程序不能同时使用它的所有对象。
图1-1演示了程序每次使用对象时所发生的操作
图1-1