今天要简单回顾一下类与对象这节,并整理两个习题。
1.类与对象的定义与访问
2.构造函数与析构函数
3.类的其他成员
4.习题整理
一.类的定义与访问
1.定义,例如:
class student
{
private:
int age;
char number;
public:
void getnumber()
{
cout<<number<<endl;
}
int getage()
{
return age;
}
};
类中有public private public三种访问属性,如果缺省默认为私有类型
2.this指针
class Date
{
public:
Date();//无参构造
Date(int y,int m,int d);//有参构造
private:
int year,month,day;
};
Date::Date()//构造函数的实现
{
year=0;
month=0;
day=0;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
class Date
{
public:
Date(int y,int m,int d):year(y),month(m),day(d)
private:
int year,month,day;
};
构造函数可以重载
2.析构函数
~student()
{
cout<<"Destructor called"<<endl;
}
3.对象复制
通过调用复制构造函数,又称为拷贝构造函数
class 类名
{
public:
类名(参数表); //构造函数
类名([const] 类名 &对象名); //复制构造函数
......
};
类名::类名([const] 类名 &对象名) {
函数语句
}
享机制。
静态成员包括:
●静态数据成员
●静态成员函数
例如:
static int total;
static void display();
class person
{
static int total;
};
int person::total=0;
利用静态成员的优点
●静态成员函数只能直接访问该类中的静态数据成员,
而不会影响该类的其他数据成员
●采用静态成员函数,可以在创建对象之前处理静态
数据成员,这是普通成员函数不能实现的
●静态成员函数在同一个类只有一个成员函数的地址
映射,节约了计算机系统的开销
●静态成员函数如要访问非静态成员,需要通过对象
名才能访问该对象的非静态数据成员
2.友元函数
友元函数可以访问类中的私有数据成员,单向性
格式:
friend 返回值类型 函数名(参数表);
例如:
class Point
{
public:
Point()
{
cout<<"请输入给定点的(x,y)坐标,以空格分开:";
cin>>x>>y;
cout<<"point:("<<x<<","<<y<<")"<<endl;
}
friend double distance(Point &p1,Point &p2); //友元函数的
声明
private:
double x,y;
};
double distance(Point &p1,Point &p2) //普通函数作为友元函数
{
double deltax,deltay;
deltax=p1.x-p2.x; //访问私有成员
deltay=p1.y-p2.y; //访问私有成员
return sqrt(deltax*deltax+deltay*deltay);
}
void main()
{
Point p1;
Point p2;
cout<<"以上两给定点之间的距离是:"<<distance(p1,p2)<<endl;
}
3.常类型(虽然学完了,但对于这部分不是很熟练,所以就简单写一点点)
格式:
const 类型 说明符&引用名;
例如:int n=8;const int &b=n;
b是一个常引用,它说明的对象不允许更改
4.类的包含
类的包含(称为has A)是程序设计中一种软件重用技术。即定义一
个新的类时,通过编译器把另一个类 “抄”进来。
当一个类中含有已经定义的类类型成员,带参数的构造函数对数据
成员初始化,须使用初始化语法形式。
构造函数 ( 变元表 ) : 对象成员1( 变元表 ) , … , 对象成员n
( 变元表 ) ;
例题:用类的包含计算两点间的距离
#include<cmath>
#include<iostream>
using namespace std;
class Point
{ public:
Point( int xi=0, int yi=0 ) { x = xi; y = yi; }
int GetX() { return x; }
int GetY() { return y; }
private: int x; int y;
};
class Distance
{ public:
Distance( Point xp1, Point xp2 );
double GetDis() { return dist; }
private:
Point p1, p2; //Point类数据成员
double dist; //记录距离的数据成员
};
Distance::Distance( Point xp1, Point xp2 ) : p1( xp1 ), p2( xp2 )
{ double x = double(p1.GetX() - p2.GetX());
double y = double(p1.GetY() - p2.GetY());
dist = sqrt(x * x + y * y);
}
int main()
{ Point mp1( 5, 10 ), mp2( 20, 30 ) ;
Distance mdist( mp1, mp2 ) ;
cout<<"The distance is "<<mdist.GetDis()<<endl;
}
四.两个习题
1、定义一个圆类(Circle),属性为半径(radius)、圆
周长和面积,操作为输入半径并计算周长、面积,输出半
径、周长和面积。要求定义构造函数(以半径为参数,缺
省值为0,周长和面积在构造函数中生成)、析构函数和
拷贝构造函数。
(后面加//代表老师讲后改动的地方)
#include<iostream>
using namespace std;
#define pi 3.14159
class Circle
{
private:
float r,c,s;
public:
Circle(float r=0)//构造函数,半径为参数,缺省值为0
{
r=r;
}
Circle(Circle&c);//复制构造函数,里面参数是引用//
~Circle()//析构函数
{
cout<<"Destructor called"<<endl;
}
void setcircle()
{
cout<<"输入圆的半径:"<<endl;
cin>>r;
c=2*pi*r;
s=pi*r*r;
}
void getr()
{
cout<<"圆的半径为:"<<r<<endl;
}
void getc()
{
cout<<"圆的周长为:"<<c<<endl;
}
void gets()
{
cout<<"圆的面积为:"<<s<<endl;
}
};
Circle::Circle(Circle &c)//
{
r=c.r;
s=c.s;//
c=c.c;//
}
int main()
{
Circle c1;
Circle c2(c1);
c1.setcircle();
c1.getr();
c1.getc();
c1.gets();
}
2、设计一个用于人事管理的People类。只抽象
所有类型人员都具有的属性:number(字符指
针)、sex、birthday、id等。其中birthday定义
为一个Date类内嵌子对象。用成员函数实现对人
员信息的录入和显示。要求包括:构造函数和析
构函数、拷贝构造函数。
(这题开始自己的错误多,先把正确的放上)
#include<iostream>
using namespace std;
class Date{
int year,month,day;
public:
Date(int y,int m,int d)
{year=y;month=m;day=d;}
friend class People;
};
class People
{
int number;
char sex;
Date birthday;
char *id;
public:
People(int num,char s,char *ID,int y,int m,int d):birthday(y,m,d)//**
{number=num;sex=s;id=new char[strlen(ID)+1];strcpy(id,ID);}
~People(){ delete []id;cout<<"Destructor"<<endl;}//重点
People(People &p):birthday(p.birthday)
{number=p.number;sex=p.sex;id=new char[strlen(p.id)+1];strcpy(id,p.id);}
void ShowInfo()
{cout<<number<<" "<<sex<<" "<<birthday.year<<"-"<<birthday.month<<"-"<<birthday.day<<" "<<id<<endl;}
};
int main()
{
People p1(1001,'M',"37090211111",1999,1,1);
p1.ShowInfo();
People p2=p1;
p2.ShowInfo();
return 0;
}
自己开始不大正确的
#include<iostream>
#include<string>
using namespace std;
class Date
{
private:
int year;
int month;
int day;
public:
Date(){}
Date(int y,int m,int d)
{
year=y;month=m;day=d;
}
void setdate()
{
cout<<"请输入成员生日的年,月,日"<<endl;
cin>>year>>month>>day;
}
void show()
{
cout<<"出生日期为"<<endl;
cout<<"年:"<<year<<"月:"<<month<<"日:"<<day<<endl;
}
若不用前两个函数,可以用友元,brithday可以直接访问
//friend class People;
};
class People
{
private:
int num;
char sex;
Date birthday;
char*id;
public:
People(){}
People(int n,char*ID,char s,int y,int m,int d):birthday(y,m,d)
{
num=n;
sex=s;
//id=i;
id=new char[strlen(ID)+1];
strcpy(id,ID)
}
People( People& p):birthday(p.birthday)//复制构造函数
{
num=p.num;
sex=p.sex;
// birthday=p.birthday;
// id=p.id;
id=new char[strlen(p.id)+1];
strcpy(id,p.id);
}
void setpeople()
{
cout<<"请输入成员信息"<<endl;
cout<<"编号:";
cin>>num;
cout<<"性别(m/f):";
cin>>sex;
cout<<"生日:";
birthday.setdate();
cout<<"身份证号:";
cin>>id;
cout<<endl;
}
void getpeople()
{
cout<<"编号:"<<num<<endl;
cout<<"性别:"<<sex<<endl;
cout<<"生日:";
birthday.show();
cout<<"身份证号:"<<id<<endl;
}
~People()//析构函数//这里也有错误,析构没写delete
{
cout<<"Destructor called"<<endl;
}
};
//主函数貌似不对1,没有复制构造
int main()
{
Date p;
People s;
p.setdate();
p.show();
s.setpeople();
s.getpeople();
return 0;
}