Design and experiment of simple classes in C language/C++ (2/5)

1. Purpose of the experiment:

1. Understand the characteristics and ideas of object-oriented programming, understand the encapsulation and information hiding of classes.

2. Master the definition and function of classes and objects, and use them proficiently.

3. Master the definition of member functions of classes, and be proficient in defining and applying member functions of classes.

4. Master the references of object members, and skillfully apply the references of object members.

5. To master the constructor and destructor, skillful application is required.

2. Experimental content

  1. Programming implementation: Please check the following program to find out the errors and correct them. Then debug it on the machine to make it run normally. Enter the values ​​of hours, minutes, and seconds from the keyboard at runtime, and check whether the output is correct.

code:

After testing (Error: [Error] 'int Time::hour' is private)

 #include <iostream>

using namespace std;

class Time

{ private:// (private member, can only be used inside the class,

   void set_time(void);// It is also defined inside the class when it is defined,

   void show_time(void);//just need to add private and public)

public:

int hour;

int minute;

   int sec;

};Time t;

int set_time(void)

cin>>t.hour;

   cin>>t.minute;

   cin>>t.sec;}

int show_time(void)

{

 cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}

int main()

{

set_time();

show_time(); }            

Program testing and running results:

  1. Run the program:

code:

/* (analysis: Student(int n, float s): num(n), score(s){} with

void change(int n,float s){num=n;score=s;}功能相同都是初始化num与score。

 而前面有了const则函数值不会改变) */

#include <iostream>

using namespace std;

class Student

{  public:

      Student(int n,float s):num(n),score(s){}

      void change(int n,float s){num=n;score=s;}

      void display(){cout<<num<<" "<<score<<endl;}

   private:

  int num;

float score;};

int main()

{  Student stud(101,78.5);

   stud.display();

   stud.change(101,80.5);

   stud.display();

   return 0;}

程序测试及运行结果:

  1. 编程实现:根据测试程序及测试程序的输出结果,编写类Welcome.

程序代码:

#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

class Weclome    //定义类

{

public:Weclome();

Weclome(Weclome &we);

void talk();

void get(char s[]);

void display();

private:

char str[15];};

void Weclome::talk(){}

void Weclome::get(char*const){}

void Weclome::display()  //定义多个调用函数

{

cout<<"Hou are you?"<<endl;

cout<<"Fine,thank you."<<endl;

cout<<"OK"<<endl;    //定义输出内容

}

Weclome::Weclome(class Weclome &)

{

cout<<"Thank you."<<endl;

}

Weclome::Weclome()

{

cout<<"Weclome"<<endl;

}

int main()    //主函数

{

Weclome we;

Weclome you(we);

you.display();

you.get("Thank you.");

you.talk();

you.display();

return 0;

}

程序测试及运行结果:

  1. 编程实现:编写一个复数类型complex,其中含有若干个成员函数,使用该类可以完成复数的加法,复数的输入输出。

程序代码:

#include <iostream>

using namespace std;

class Complex

{public:

    Complex(){real=0; imag=0;}         //默认构造函数

   Complex(double r){real=r; imag=0;}        //转换构造函数

    Complex(double r, double i){real=r; imag=i;}   //构造函数

    friend Complex operator+(Complex c1, Complex c2);  //重载运算符+函数为友元函数

    void display();        //定义display函数

private:

    double real; double imag;}; //重载运算符+函数

Complex operator+(Complex c1, Complex c2)

{ return Complex(c1.real+c2.real, c1.imag+c2.imag);}

//display函数

void Complex::display()

{ cout<<real; if (imag>=0) cout<<"+"; cout<<imag<<"i"<<endl;}

int main(){

    Complex c1(1, -2), c2(3, 4), c3;   //定义Complex类对象c1,c2,c3

    int i;c3=c1+c2; c3.display(); cout<<"Please enter number: ";

cin>>i; c3=i+c1; c3.display(); c3=c1+i; c3.display(); return 0; }

程序测试及运行结果:

  1. 编程实现:定义一个简单的时间类TimeType,它具有数据成员hms,用来表示当前时间的时、分、秒。而后设计该类欲实现的功能。

程序代码:

#include<iostream>

using namespace std;

class TimeType

{private:

    int hour;  int min; int sec;

public:

    void settime(); void puttime();}t;

void TimeType::settime()//加inline使其变为内联函数,替换作用

{ cin>>hour;

    cin>>min; cin>>sec;}

void TimeType::puttime()

{if(sec>=60){min+=sec/60;sec=sec%60;}

    if(min>=60){hour+=min/60;min=min%60;}

    if(hour>=24) hour=hour%24;

    cout<<hour<<":"<<min<<":"<<sec<<endl;

}int main()

{   TimeType t;//t是time类型的对象

    t.settime(); t.puttime();

    return 0;}

程序测试及运行结果:

  1. 编程实现:设计一个点类Point,求两个点之间的距离.

程序代码:

#include<iostream>

#include<cmath>

using namespace std;

class Point

 {

    public:

       Point(int x=0,int y=0):x(x),y(y){}

//用来储存两点的数据

       float dist(Point &p);

    private:

       int x,y;

};//定义了一个类

float Point::dist(Point &p1){

    double x=this->x-p1.x;

    double y=this->y-p1.y;

    return static_cast<float>(sqrt(x*x+y*y));

}

int main(){

    Point p1(2,2),p2(5,5);   //所要求的两点

    cout<<"(2,2)和(5,5)之间距离="<<p1.dist(p2)<<endl;//输出

    return 0;

}

程序测试及运行结果:

分析与讨论:

  1. 什么是封装?类是如何进行封装的?

答:封装,就是把一系列的数据放在一个类中

利用public和private对象隐藏其内部数据,使得只有对象的预期部分以编程方式可访问。

2、类的成员函数与普通的函数最本质的区别是什么?

答:

成员函数一般是按类型传递,也即是传指针地址普通函数可以随便调用,并且无法继承和封装,成员函数根据类的不同,可以进行继承,根据公有私有的不同,调用方式也不同。

3、构造函数如何命名?析构函数如何命名?

答:

构造函数的名字必须与类名同名,它不具有任何类型,不返回任何值。构造函数的功能是由用户定义的,用户根据初始化的要求设计函数体和函数参数。在类对象进入其作用域时调用构造函数。构造函数没有返回值,因此也不需要在定义构造函数时声明类型。

4、构造函数和析构函数的作用是什么?

答:

构造函数可用于对象的初始化操作,当对象调用类时,就自动力调用了构造函数,构造函数可以自定义,也可以默认。

析构函数是释放对象调用的内存空间。构造函数可以是多个,析构函数一至少只有一个。

5、什么时候需要自己定义构造函数和析构函数?

答:C++中构造函数或析构函数定义为private

Guess you like

Origin blog.csdn.net/qq_59819866/article/details/131437732