《C++从入门到精通》实例--类和对象
//范例1:关于日期类声明和定义的例子
//知识点:类的声明和定义
//2018.3.21
#include <iostream>
using namespace std;
//下面为类的声明
class Cdate
{
public: //下面定义3个公有成员均为成员函数
void SetDate(int y, int m, int d); //设置日期,用它使对象获取数值
int IsLeapYear(); //用来判断是否闰年的函数
void print(); //用来将年、月、日的具体值输出
private:
int year, month, day; //定义3个int型变量的私有成员
};
//下面为日期类的实现部分
void Cdate::SetDate(int y, int m, int d) //设置日期使对象获取数据
{
year = y;
month = m;
day = d;
}
int Cdate::IsLeapYear() //用来判断是否闰年的函数
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); //如为闰年返回1
}
void Cdate::print() //用来将年、月、日的具体值输出
{
cout << year << "," << month << "," << day << endl;
}
int main(int argc, char* argv[])
{
int rn;
Cdate date1;//声明对象
date1.SetDate(2004, 12, 30);
rn = date1.IsLeapYear();
if (rn == 1)
cout << "闰年" << endl;
date1.print();
system("pause");
return 0;
}
//范例2:使用对象完成计算矩形面积的例子
//知识点:对象的定义和使用
//2018.3.22
#include <iostream>
using namespace std;
//下面为类的声明部分
class Carea
{
private:
int x, y; //声明两个私有变量x和y
public:
void set_value(int a, int b); //声明设置矩形长宽值的函数,用来为私有变量赋值
int area(); //声明计算面积的成员函数
};
//下面为矩形面积类的实现部分
void Carea::set_value(int a, int b) //设置矩形长宽值
{
x = a; //私有变量x获取数据
y = b; //私有变量y获取数据
}
int Carea::area()
{
return (x*y);
}
int main(int argc, char* argv[])
{
Carea rect1, rect2; //声明两个对象rect1和rect2
rect1.set_value(3, 4); //为对象rect1的成员函数的参数赋值
rect2.set_value(5, 6);
cout << "rect1.area:" << rect1.area() << endl;
cout << "rect2.area:" << rect2.area() << endl;
system("pause");
return 0;
}
//范例3:构造函数的例子
//知识点:构造函数
//2018.3.22
#include <iostream>
using namespace std;
class Carea
{
private:
int width, height;
public:
Carea(int, int); //构造函数声明
int area(); //计算面积的成员函数的声明
};
//以下为构造函数定义
Carea::Carea(int a, int b)
{
width = a;
height = b;
}
int Carea::area()
{
return(width*height);
}
int main(int argc, char* argv[])
{
Carea rect1(3, 4); //创建rect1对象并调用构造函数进行初始化
Carea rect2(5, 6);
cout << "rect1 area:" << rect1.area() << endl;
cout << "rect2 area:" << rect2.area() << endl;
system("pause");
return 0;
}
//范例4:调用构造函数输出学生的学号、姓名和性别
//知识点:构造函数
//2018.3.22
#include <iostream>
#include <string>
using namespace std;
class Stud
{
private:
int num;
char name[10];
char sex;
public:
Stud(); //构造函数
void display();
};
Stud::Stud()
{
num = 10010; //给数据赋初值
strcpy_s(name, "suxl");
sex = 'F';
}
void Stud::display()
{
cout << "学号:" << num << endl;
cout << "姓名:" << name << endl;
cout << "性别" << sex << endl;
}
int main(int argc, char* argv[])
{
Stud stud1; //定义对象stud1时自动执行构造函数
stud1.display();
system("pause");
return 0;
}
PS: strcpy_s
//范例5:构造函数重载的例子
//知识点:构造函数重载
//2018.3.22
#include <iostream>
#include <string>
using namespace std;
class Carea
{
private:
int width, height;
public:
Carea(); //构造函数的声明
Carea(int, int); //重载构造函数的声明
int area(); //计算面积的求值函数的声明
};
//无参构造函数的定义
Carea::Carea()
{
width = 5;
height = 5;
}
//带两个参数的构造函数的定义
Carea::Carea(int a, int b)
{
width = a;
height = b;
}
int Carea::area()
{
return (width*height);
}
int main(int argc, char* argv[])
{
Carea rect1(3, 4);//创建对象并调用构造函数进行初始化
Carea rect2; //创建对象并调用重载构造函数进行初始化
//注意:声明一个新对象时,若不想传入参数,则不需写括号:Carea rect2();是错误写法
cout << "rect1 area:" << rect1.area() << endl;
cout << "rect2 area:" << rect2.area() << endl;
system("pause");
return 0;
}
//范例6:带默认参数的构造函数重载
//知识点:构造函数重载
//2018.3.22
#include <iostream>
#include <string>
using namespace std;
class CDate //以下为类的定义,定义了带默认参数的构造函数
{
private:
int nYear, nMonth, nDay; //私有成员定义
public:
CDate(int year = 2002, int month = 7, int day = 30) //构造函数定义
{
nYear = year; nMonth = month; nDay = day; //变量赋初值
}
void display()
{
cout << nYear << "-" << nMonth << "-" << nDay << endl;
}
};
int main(int argc, char* argv[])
{
CDate day1; //创建对象并使用默认参数调用构造函数
CDate day2(2002, 8); //创建对象并使用默认参数构造函数重载
day1.display();
day2.display();
system("pause");
return 0;
}
//范例7:
//知识点:析构函数
//2018.3.23
#include <iostream>
using namespace std;
class Carea
{
int *width, *height; //默认为私有变量
public:
Carea(int, int); //构造函数的声明
~Carea(); //析构函数的声明
int area(); //计算面积的成员函数
};
// 以下为构造函数的定义
Carea::Carea(int a, int b)
{
width = new int; //定义一个整型指针变量width
height = new int; //定义一个整型指针变量height
*width = a;
*height = b;
}
//以下为析构函数的定义
Carea::~Carea()
{
delete width; //释放指针变量width占用的内存资源
delete height;
cout << "释放对象占用的内存资源" << endl; //调用析构函数的信息提示
}
int Carea::area() //计算面积的成员函数实现
{
return ((*width)*(*height));
}
int main(int argc, char* argv[])
{
Carea rect1(3, 4), rect2(5, 6);
cout << "rect1 area:" << rect1.area() << endl;
cout << "rect2 area:" << rect2.area() << endl; //注意:在vs输出中看不到析构函数的输出"释放对象占用的内存资源",只有在命令行中可看到
system("pause");
return 0;
}
//范例8:析构函数的调用次序例子
//知识点:析构函数
//2018.3.23
#include <iostream>
using namespace std;
class Test //定义类
{
private:
int num;
public:
Test(int a) //定义构造函数
{
num = a;
cout << "第" << num << "个Test对象的构造函数被调用" << endl;
}
~Test() //定义析构函数
{
cout << "第" << num << "个Teat对象的析构函数被调用" << endl;//注意:在vs输出中看不到析构函数的输出信息,只有在命令行中可看到
}
};
int main(int argc, char* argv[])
{
cout << "进入main()函数" << endl;
Test t[4] = { 0, 1, 2, 3 }; //定义4个对象,分别以0,1,2,3赋给构造函数的形参a
cout << "main()函数在运行中" << endl;
cout << "退出main()函数" << endl;
system("pause");
return 0;
}
//范例9:友元函数的应用
//知识点:友元函数
//2018.3.23
#include <iostream>
using namespace std;
class CPoint //定义类
{
public:
CPoint(unsigned x, unsigned y) //定义构造函数
{
m_x = x;
m_y = y;
}
void print()
{
cout << "Point(" << m_x << "," << m_y << ")" << endl;
}
friend CPoint inflate(CPoint &pt, int nOffset);//声明一个友元函数
private:
unsigned m_x, m_y; //定义私有成员变量
};
CPoint inflate(CPoint &pt, int nOffset) //友元函数的定义
{
CPoint ptTemp = pt;
ptTemp.m_x += nOffset; //直接改变私有数据成员m_x的值
ptTemp.m_y += nOffset;
return ptTemp; //返回修改过私有成员值的类对象
}
int main(int argc, char* argv[])
{
CPoint pt(10, 20); //创建对象并调用构造函数初始化
pt.print(); //输出修改前的类对象pt的私有变量值
pt = inflate(pt, 3); //调用友元函数
pt.print(); //输出修改后的类对象pt的私有变量值
system("pause");
return 0;
}
//范例10:友元成员的应用
//知识点:友元成员
//2018.3.23
#include <iostream>
#include <string>
using namespace std;
class Y; //为向前引用
class X
{
int x;
char *strx; //定义私有成员
public:
X(int a, char *str) //定义构造函数
{
x = a;
strx = new char[strlen(str) + 1]; //分配空间
strcpy_s(strx, strlen(str) + 1,str); //调用字符串拷贝函数
}
void show(Y &ob); //声明公有成员函数
};
class Y
{
int y;
char *stry;
public:
Y(int b, char *str)
{
y = b;
stry = new char[strlen(str) + 1];
strcpy_s(stry, strlen(str) + 1, str); //使用strcpy_s,new出来的字符串,提示不支持两个参数,所以必须用三个参数
}
friend void X::show(Y &ob); //声明友元成员
};
void X::show(Y &ob) //定义友元成员
{
cout << "the string of X is:" << strx << endl;
cout << "the string of Y is:" << ob.stry << endl;
}
int main(int argc,char* argv[])
{
X a(10, "stringx"); //创建类X的对象
Y b(10, "stringy"); //创建类Y的对象
a.show(b);
system("pause");
return 0;
}
PS: C++安全函数之strcpy_s
//范例11:友元类
//知识点:友元类
//2018.3.23
#include <iostream>
#include <string>
using namespace std;
class X //定义类X
{
private: //定义私有成员
int x;
static int y;
friend class Y; //声明类Y为类X的友元类
public:
void set(int a) { x = a; } //定义公有成员函数
void print() { cout << "x=" << x << "," << "y=" << y << endl; }
};
class Y //定义类Y
{
private:
X a;
public:
Y(int m, int n) //定义构造函数
{
a.x = m; //初始化私有变量的值
X::y = n;
}
void print(); //声明友元成员
};
int X::y = 1;
void Y::print() //定义友元成员
{
cout << "x=" << a.x << ",";
cout << "y=" << X::y << endl;
}
int main(int argc, char* argv[])
{
X b; //创建类X的对象b
b.set(5); //调用对象b的成员函数进行赋值
b.print(); //调用对象b的成员函数输出值
Y c(6, 9); //创建类Y的对象c,并进行初始化
c.print(); //调用友元成员进行输出
b.print(); //调用对象b的成员函数进行输出
system("pause");
return 0;
}
//综合运用:设计一个Bank类,实现银行某账号的资金往来账目管理,包括建账号、存入、取出等
//知识点:类和对象
//2018.3.24
#include <iostream>
#include <string>
using namespace std;
#define Max 100 //数值元素个数最大值
class Bank //定义类
{
int top;
char date[Max][10]; //日期
int money[Max]; //存取金额
int rest[Max]; //余额
static int sum; //累计余额
public:
Bank() { top = 0; } //构造函数
void bankin(char d[], int m) //处理存入帐成员函数
{
strcpy_s(date[top], d); //传递日期值
money[top] = m; //存帐金额
sum = sum + m; //计算余额
rest[top] = sum; //余额值赋给数值元素
top++; //数值元素下标加1
}
void bankout(char d[], int m) //处理取出帐成员函数
{
strcpy_s(date[top], d); //传递日期值
money[top] = -m; //取帐金额
sum = sum -m; //计算余额
rest[top] = sum; //余额值赋给数值元素
top++; //数值元素下标加1
}
void disp(); //打印明细成员函数
};
int Bank::sum = 0; //初始化静态变量为0
void Bank::disp()
{
int i;
cout << ("日期.........存入.........取出.........余额\n");
for (i = 0; i < top; i++) //打印明细表
{
cout << date[i];
if (money[i] < 0)
cout << ".................." << -money[i] << "..........";
else
cout << "....." << money[i] << "......................";
cout << rest[i] << endl;
}
}
int main(int argc, char* argv[])
{
Bank obj; //创建对象
obj.bankin("2018.1.1", 1000); //调用对象的存入帐成员函数
obj.bankin("2018.2.1", 2000);
obj.bankout("2018.3.1", 500);
obj.bankout("2018.4.1", 800);
obj.disp();
system("pause");
return 0;
}
注意:
1. 类是抽象的,不占用内存。而对象是具体的,占用存储空间。这里所说的类不占空间,对象占用空间,应该是从栈或堆的角度出发的。类是抽象的不占用栈和堆的空间,成员函数的代码要占用代码段的空间。静态成员会占用全局区的空间。但是类的对象会占用栈或堆的空间。