前言:单冒号和双冒号的区别
实验一
一.实验目的
1.理解并掌握类和对象的含义及定义方法;
2.理解类成员访问控制的含义及公有、私有和保护成员的区别;
3.理解并掌握构造函数和析构函数的概念及使用方法;
4.理解并掌握复制构造函数的作用和方法;
5.理解并掌握类的组合的含义及使用方法;
6.理解并掌握静态数据成员和静态成员函数的含义及使用方法;
7.理解并掌握友元类和友元函数的含义及使用方法;
1.阅读程序回答问题。
#include <iostream>
using namespace std;
class CPerson
{
public:
void Print();
private:
CPerson();
private:
int age;
char *name;
};
CPerson::CPerson()
{
}
void CPerson::Print()
{
cout<<"name="<<name<<",age="<<age<<endl;
}
void main()
{
CPerson ps(23,"张三");
ps.Print();
}
上述程序是否正确,若存在错误,在不改变主函数内容的前提下,请给出改正意见。
答:不正确。
a、把CPoint类中的构造函数的private直接去掉;(原因就是在类外无法访问private成员);
b、把无参构造函数变为有参构造函数,如下:
public:
void Print();
CPerson(int age, char *name);
2.类Person的定义如下,请实现该类,并在主函数中创建对象obj,然后使用构造函数为obj赋予初始值(内容自定)。
class Person
{
public:
Person(char *xname,int xage,int xsalary,char *xtel);
void disp();
private:
char name[10];
int age;
int salary;
char tel[8];
};
答:源代码:
Person::Person(char *xname,int xage,int xsalary,char *xtel){
strcpy(name,xname);//字符指针转为字符数组
age = xage;
salary = xsalary;
strcpy(tel,xtel);
}
void Person::disp(){
char *na = name;//字符数组转为字符指针
char *te = tel;
cout<<"name = "<<na<<"\nage = "<<age<<"\nsalary = "
<<salary<<"\ntel = "<<te<<endl;
}
int main(){
Person pe("赵佳佳",24,10000,"19127480");
pe.disp();
return 0;
}
运行结果截图:
3. 分析并比较下列程序运行的结果。
程序一:
#include<iostream.h>
using namespace std;
class smallone
{
public:
smallone(int sma)
{ x=sma;
cout<<"sm constr:"<<sma<<"\n";
}
int getX()
{
return x;
}
~smallone()
{
cout<<"调用了析构函数释放对象!\n";
}
private :
int x;
};
void fn(int n)
{ smallone sm(n);
cout<<"in function fn with n="<<n<<" sm.x="<<sm.getX()<<endl;
}
int main()
{ fn(10);
fn(20);
return 0;
}
程序二:
#include<iostream.h>
using namespace std;
class smallone
{
public:
smallone(int sma)
{ x=sma;
cout<<"sm constr:"<<sma<<"\n";
}
int getX()
{
return x;
}
~smallone()
{
cout<<"调用了析构函数释放对象!\n";
}
private :
int x;
};
void fn(int n)
{ static smallone sm(n);
cout<<"in function fn with n="<<n<<" sm.x="<<sm.getX()<<endl;
}
int main()
{ fn(10);
fn(20);
return 0;
}
(1)将上述两个程序运行结果的截图粘贴在下面,并分析结果不同的原因。
答:A、程序一、二中头文件<iostream.h>应该变为<iostream>,否则报错
运行结果:
B、程序二
分析:程序二较程序一区别在与,程序二的用smallone 创建的对象是static修饰的,而静态变量特点其一就是,只被初始化一次。
运行结果:
4. 设计实现一个Point类,满足以下要求:
a. 该类包含两个整型成员变量x(横坐标)和y(纵坐标),以及一个输出函数Print()用来输出横坐标和纵坐标,要求不可以在类的外部直接访问成员变量;
b.可以采用没有参数的构造函数初始化对象,此时的成员变量采用默认值0;
c.可以采用直接输入参数的方式来初始化该类的成员变量;
d.可以采用其它的Point对象来初始化该类的成员变量;
e.设计一个主函数来测试以上功能。
答:源代码:
#include<iostream>
using namespace std;
class Point{
public:
Point(int x = 0,int y = 0);
Point(Point &p); //复制构造函数
void Print();
private:
int x;
int y;
};
Point::Point(int x,int y){
this->x = x;
this->y = y;
}
Point::Point(Point &p){
x=p.x;
y=p.y;
}
inline void Point::Print(){
cout<<"x = "<<x<<",y = "<<y<<endl;
}
int main(){
int x,y;
Point p1;
cout<<"无参构造初始化对象"<<endl;
p1.Print();
cout<<"有参构造初始化对象,请输入x y(空白符隔开):";
cin>>x>>y;
Point p2(x,y);
p2.Print();
cout<<"复制之前有参构造的对象"<<endl;
Point p3(p2);
cout<<"复制完成,打印"<<endl;
p3.Print();
return 0;
}
运行结果截图:
5. 类的组合
根据已经给出的class Point类,完成以下内容:
(1)定义一个class Distance类,其中含有3个私有数据成员和一个成员函数,3个私有成员数据是: Point类的两个点p1,p2和两点间的距离double dist;类的成员函数为:double GetDis(); 其作用是:返回类成员dist的数据;
(2)给出组合类class Distance的构造函数;
(3)主函数以:Point myp1(1,1), myp2(4,5);Distance myd(myp1, myp2);进行测试,输出myp1(1,1), myp2(4,5)两点间距离。
给出以上要求,补充完整的程序源代码,并粘贴运行结果截图。
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int xx,int yy) { X=xx; Y=yy; } //构造函数
Point(Point &p);
int GetX(void) { return X; } //取X坐标
int GetY(void) { return Y; } //取Y坐标
private:
int X,Y; //点的坐标
};
Point::Point(Point &p)
{
X = p.X;
Y = p.Y;
cout << "Point拷贝构造函数被调用" << endl;
}
class Distance //补充类的定义
{
};
Distance::Distance{ } // 补充组合类的构造函数
int main()
{
Point myp1(1,1), myp2(4,5);
Distance myd(myp1, myp2);
cout << "The distance is:";
//补充输出两点间距离
return 0;
}
答:源代码:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int xx, int yy) { X = xx; Y = yy; } //构造函数
Point(Point &p);
int GetX(void) { return X; } //取X坐标
int GetY(void) { return Y; } //取Y坐标
private:
int X, Y; //点的坐标
};
Point::Point(Point &p)
{
X = p.X;
Y = p.Y;
cout << "Point拷贝构造函数被调用" << endl;
}
class Distance //补充类的定义
{
public:
Distance(Point xp1, Point xp2);
double GetDis() { return dist; }
private:
Point p1;
Point p2;
double dist;
};
Distance::Distance(Point xp1,Point xp2):p1(xp1),p2(xp2) {// 补充组合类的构造函数
int x = p1.GetX() - p2.GetX();
int y = p1.GetY() - p2.GetY();
dist = sqrt(x*x + y*y);
}
int main()
{
Point myp1(1, 1), myp2(4, 5);
Distance myd(myp1, myp2);
cout << "The distance is:";
cout << myd.GetDis() << endl;
system("pause");
}
运行结果截图:
6.类的静态成员
设计并测试一个名为Rectangle的矩形类,其属性为矩形的左下角和右上角两个点的坐标;
(1)根据坐标能计算矩形的面积
(2)增加静态成员counter ,使之能统计出创建的矩形的个数
给出程序源代码,并粘贴运行结果截图。
答:源代码:
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int xx, int yy) { X = xx; Y = yy; } //构造函数
int GetX(void) { return X; } //取X坐标
int GetY(void) { return Y; } //取Y坐标
private:
int X, Y; //点的坐标
};
class Rectangle
{
public:
Rectangle(Point xleft, Point xright);
double GetArea() { return area; }
static void showCount() { cout << "Object count = " << counter << endl; }
~Rectangle() { --counter; }
private:
Point left;
Point right;
double area;
static int counter;
};
int Rectangle::counter = 0;
Rectangle::Rectangle(Point xleft, Point xright) :left(xleft), right(xright) {// 组合类的构造函数
int x = left.GetX() - right.GetX();
int y = left.GetY() - right.GetY();
area = abs(x * y);
++counter;
}
int main()
{
Point left1(0, 0), right1(4, 5);
Point left2(1, 1), right2(5, 7);
Rectangle r1(left1, right1), r2(left2, right2);
cout << "第一个矩阵面积: " << r1.GetArea() << endl;
cout << "第二个矩形面积: " << r2.GetArea() << endl;
Rectangle::showCount();
system("pause");
}
运行结果截图:
7.类的友元函数
定义一个Circle类,数据成员为:radius(半径),成员函数为:getArea()算圆的面积;另外设计其友元函数calArea();构造一个Circle对象,并测试用成员函数getArea()和友元函数calArea()计算的结果,给出程序源代码,并粘贴运行结果截图。
答:
源代码:
#include <iostream>
#include <cmath>
#define PI 3.14159
using namespace std;
class Circle {
public:
Circle(double xradius) { radius = xradius; }
double GetArea();
friend double calArea(Circle c);
private:
double radius;
};
double Circle::GetArea() {
return PI * pow(radius, 2);
}
double calArea(Circle c) {
return PI * pow(c.radius, 2);
}
int main() {
Circle c(2);
cout << "Radius:" << 2 << ", PI = " << 3.14159 << endl;
cout << "成员函数GetArea()计算 The area is : " << c.GetArea() << endl;
cout << "友元函数calArea()计算 The area is : " << calArea(c) << endl;
system("pause");
}
运行结果截图: