C++__(类和对象的特性)

本文介绍了C++中对象和类的基本定义方法,探讨了成员函数的多种定义方式及类的封装性。并通过实例展示了如何利用类实现时间的输入输出、数组最大值查找等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:定义对象的方法:
//1.先声明类类型,再定义对象
class Student()
{
    private: 
        int num; 
        char name[20]; 
        char sex; 
    public: 
        void display() 
    { 
        cout<<"num:"<<num<<endl; 
        cout<<"name:"<<name<<endl;
        cout<<"sex:"<<sex<<endl; 
    }
};
Student stud1,stud2;


//2.在声明类的同时定义对象
class Student()
{
    public: 
        void display() 
    { cout<<"num:"<<num<<endl;
      cout<<"name:"<<name<<endl; 
      cout<<"sex:"<<sex<<endl; 
    }
private: 
    int num; 
    char name[20];
    char sex;
 }
 stud1,stud2;

//3.在类外定义成员函数(前面两个,成员函数是在类体中定义的,此处是在类体中只对成员函数进行生声明,然后在类的外面进行函数定义.)
calss Student
{
    public: 
        void display();
    private: 
        int num; 
        string name; 
        char sex;
};

void Student::display()
{ 
    cout<<"num:"<<num<<endl; 
    cout<<"name:"<<name<<endl;
     cout<<"sex:"<<sex<<endl;
}Student stu1,stu2;


二.类的成员函数:

1.内置成员函数

2.成员函数

三.对象成员的引用

1.通过对象名和成员运算符访问对象中的成员

访问对象中成员的一般形式为:

对象名.成员名
stu1.num=1001;//假设num已经定义为对象stu1的公用的整形数据成员
stu1.display(); //调用对象stu1的公用成员函数

一定要指定对象名,否则会出错,或者被当成普通函数处理.

注意:在类外只能调用公用的成员函数。在一个类中应当至少有一个公用的成员函数,作为对外的接口,否则无法对对象进行任何操作.

2.通过指向对象的指针访问对象中的成员

class Time
{
public:          //数据成员是公用的
    int hour;
    int minute;
};
Time t,*p;       //定义对象t和指针变量p
p=&t;            //使p指向对象t
cout<<p->hour;   //输出p指向的对象中的成员hour
p->hour表示p当前指向的对象t中的成员hour,
(*p).hour也是对象t中的成员hour,因为(*p)就是对象t,
在p指向t的前提下,p->hour,(*p).hour和t.hour三者等价.


3.通过对象的引用访问对象中的成员

如果为一个对象定义了一个引用,表示它是一个对象的“别名”,
因此完全可以通过引用来访问对象中的成员,其概念和方法与通过对象中的成员是相同的。

如果已经声明了Time类,并有以下定义语句:
Time t1;        //定义对象t1
Time &t2=t1;    //定义Time类引用t2,并使之初始化为t1

cout<<t2.hour;  //输出对象t1中的成员hour

由于t2指向t1的存储单元(t2是t1的别名),因此t2.hour就是t1.hour;

四:类的封装性和信息隐蔽

1.公用接口与私有实现的分离

2.类声明与成员函数定义的分离

3.面向对象程序设计中的几个名词

五:类和对象的简单应用举例

1.用类来实现输入和输出时间

//用类来实现输入和输出时间
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

class Time    //声明Time类   
{
public:       //数据成员为公用
    int hour;
    int minute;
    int sec;
};

int main()
{
    Time t1;        //定义t1为Time类对象
    cin>>t1.hour;
    cin>>t1.minute;
    cin>>t1.sec;
    cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
    return 0;
}

/*
11 43 50
11:43:50

Process returned 0 (0x0)   execution time : 10.984 s
Press any key to continue.

*/

2.用Time类,定义多个类对象,分别输入和输出对象中的时间

//用Time类,定义多个类对象,分别输入和输出对象中的时间
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

class Time    //声明Time类
{
public:       //数据成员为公用
    int hour;
    int minute;
    int sec;
};

int main()
{
    Time t1;        //定义t1为Time类对象
    cin>>t1.hour;
    cin>>t1.minute;
    cin>>t1.sec;
    cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;

    Time t2;
    cin>>t2.hour;
    cin>>t2.minute;
    cin>>t2.sec;
    cout<<t2.hour<<":"<<t2.minute<<":"<<t2.sec<<endl;

    return 0;
}

/*
11 29 30
11:29:30
11 29 50
11:29:50

Process returned 0 (0x0)   execution time : 13.646 s
Press any key to continue.


*/

3.减少冗长,使用函数来进行输入和输出

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

class Time    //声明Time类
{
public:       //数据成员为公用
    int hour;
    int minute;
    int sec;
};

int main()
{
    void set_time(Time&t);
    void show_time(Time&t);//函数声明

    Time t1;         //定义t1位Time类对象
    set_time(t1);    //调用set_time函数,向t1对象中的数据成员输入数据
    show_time(t1);   //调用show_time函数,输出t1对象中的数据

    Time t2;
    set_time(t2);
    show_time(t2);
    return 0;
}

void set_time(Time&t)//定义函数set_time,形参t是引用变量
{
    cin>>t.hour;
    cin>>t.minute;
    cin>>t.sec;
}

void show_time(Time&t)//定义函数show_time,形参t是引用变量
{
    cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}


/*
11 22 33
11:22:33
11 22 44
11:22:44

Process returned 0 (0x0)   execution time : 8.084 s
Press any key to continue.

*/




4.对上面的程序进行修改,数据成员表的值不再由键盘输入,而是在调用函数时由实参给出,并在函数中使用默认参数。

//对上面的程序进行修改,数据成员表的值不再由键盘输入,而是在调用函数时由实参给出,并在函数中使用默认参数。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
class Time    //声明Time类
{
public:       //数据成员为公用
    int hour;
    int minute;
    int sec;
};


int main()
{
    void set_time(Time&t,int hour=0,int minute=0,int sec=0);//函数声明,指定默认参数
    void show_time(Time&);//函数声明

    Time t1;         //定义t1位Time类对象
    set_time(t1,12,23,34);   //通过实参传递时分秒的值
    show_time(t1);

    Time t2;
    set_time(t2);    //采用默认的时分秒的值
    show_time(t2);
    return 0;
}

void set_time(Time&t,int hour,int minute,int sec)//定义函数set_time,形参t是引用变量
{
    t.hour=hour;
    t.minute=minute;
    t.sec=sec;
}

void show_time(Time&t)//定义函数时不必再指定默认参数
{
    cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
/*
12:23:34
0:0:0

Process returned 0 (0x0)   execution time : 0.574 s
Press any key to continue.

*/





5.以上两个程序中定义的类都只有数据成员,没有成员函数。这显然没有体现出使用类的优越性,下面的例子,类体中就包含成员函数
//以上两个程序中定义的类都只有数据成员,没有成员函数。这显然没有体现出使用类的优越性,
//下面的例子,类体中就包含成员函数

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
class Time    //声明Time类
{
public:
    void set_time();
    void show_time();//公用成员函数
private:
    int hour;   //数据成员为私有
    int minute;
    int sec;
};


int main()
{
    Time t1;         //定义t1位Time类对象
    t1.set_time();   //调用对象t1的成员函数set_time,向t1的数据成员输入数据
    t1.show_time();

    Time t2;
    t2.set_time();
    t2.show_time();

    return 0;
}

void Time::set_time()//在类外定义set_time函数
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time()//在类外定义show_time函数
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
/*
11 22 33
11:22:33
11 33 44
11:33:44

Process returned 0 (0x0)   execution time : 9.892 s
Press any key to continue.

*/

6.找出一个整型数组中元素的最大值
#include<iostream>
using namespace std;

class  Array_max
{
public:
    void set_value();
    void max_value();
    void show_value();
private:
    int array[10];
    int max;
};

void Array_max::set_value()
{
    int i;
    for(i=0;i<10;i++)
    {
        cin>>array[i];
    }
}

void Array_max::max_value()
{
    int i;
    max=array[0];
    for(i=1;i<10;i++)
        if(array[i]>max)
        max=array[i];
}

void Array_max::show_value()
{
    cout<<"max="<<max;
}

int main()
{
    Array_max arrmax;
    arrmax.set_value();
    arrmax.max_value();
    arrmax.show_value();
    return 0;
}


/*
0 2 3 1 4 5 6 7 8 9
max=9
Process returned 0 (0x0)   execution time : 11.300 s
Press any key to continue.

*/
课后习题:

1.请检查下面程序,找出其中的错误(先不要上机,在纸面上作人工检查),并改正。然后上机调试,使之能正常运行。运行时从键盘输入时、分、秒的值,检查输出是否正确。

#include <iostream>
using namespace std;
class Time

void set_time(void);
void show_time(void);
int hour;
int minute;
int sec;
};
Time t;
int main( )
{
set_time( );
show_time( );
return 0;
}
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;

}

修改后:

#include <iostream>
using namespace std;
class Time
 {public:                    //成员改为公用的
    int hour;
    int minute;
    int sec;
  };
Time t;
void set_time(void)          //在main函数之前定义
 {
  cin>>t.hour;
  cin>>t.minute;
  cin>>t.sec;
 }

void show_time(void)         //在main函数之前定义
 {
  cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
 }
int main()
{set_time();
 show_time();
 return 0;
}
2.改写例1的程序,要求:
(1) 将数据成员改为私有的
(2) 将输入和输出的功能改为由成员函数实现;
(3) 在类体内定义成员函数。
#include <iostream>
using namespace std;
class Time
{public:
    void set_time(void)
    {
         cin>>hour;
         cin>>minute;
         cin>>sec;
    }
    void show_time(void)
    {
         cout<<hour<<":"<<minute<<":"<<sec<<endl;
    }
   private:
       int hour;
       int minute;
       int sec;
};
Time t;
int main()
{
  t.set_time();
  t.show_time();
  return 0;
}
3.在第2题的基础上进行如下修改: 在类体内声明成员函数,而在类外定义成员函数。
#include <iostream>
using namespace std;
class Time
{
public:
    void set_time(void);
    void show_time(void);
private:
    int hour;
    int minute;
    int sec;
};

void Time::set_time(void)
{
    cin>>hour;
    cin>>minute;
    cin>>sec;
}

void Time::show_time(void)
{
    cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Time t;
int main()
{
    t.set_time();
    t.show_time();
    return 0;
}

4.在第8.3.3节中分别给出了包含类定义的头文件student.h,包含成员函数定义的源文件student.cpp以及包含主函数的源文件main.cpp。请完善该程序,在类中增加一个对数据成员赋初值的成员函数set_value。上机调试并运行。

main.cpp

#include <iostream>
#include"student.h"
using namespace std;
int main()
{
 Student stud;
 stud.set_value();
 stud.display();
 return 0;
}


student.cpp

#include <iostream>
#include"student.h"
using namespace std;                   //不要漏写此行
void Student::display( )
{ cout<<"num:"<<num<<endl;
  cout<<"name:"<<name<<endl;
  cout<<"sex:"<<sex<<endl;
}
void Student::set_value()
{ cin>>num;
  cin>>name;
  cin>>sex;
}


student.h

class Student
{ public:
    void display( );
 void set_value();
  private:
    int num;
    char name[20];
    char sex ;
  };

5.将例8.4 改写为一个多文件的程序: 
(1) 将类定义放在头文件arraymax.h中;
(2) 将成员函数定义放在源文件arraymax.cpp中;
(3) 主函数放在源文件file1.cpp中。 请写出完整的程序,上机调试并运行。

arraymax.h
class Array_max
{public:
   void set_value();
   void max_value();
   void show_value();
 private:
   int array[10];
   int max;
};


file1.cpp

#include <iostream>
#include "arraymax.h"
int main()
 {Array_max  arrmax;
  arrmax.set_value();
  arrmax.max_value();
  arrmax.show_value();
  return 0;
 }


arraymax.cpp

#include <iostream>
using namespace std;
#include "arraymax.h"
void Array_max::set_value()
 { int i;
   for (i=0;i<10;i++)
     cin>>array[i];
 }
void Array_max::max_value()
 {int i;
  max=array[0];
  for (i=1;i<10;i++)
   if(array[i]>max) max=array[i];
  }
void Array_max::show_value()
 {cout<<"max="<<max<<endl;
 }
6.需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能:
(1) 由键盘分别输入3个长方柱的长、宽、高;
(2) 计算长方柱的体积;
(3) 输出3个长方柱的体积。

请编程序,上机调试并运行。

#include <iostream>
using namespace std;
class Box
{
public:
    void get_value();
    float volume();
    void display();
public:
    float lengh;
    float width;
    float height;
};


void Box::get_value()
{
    cout<<"please input lengh, width,height:";
    cin>>lengh;
    cin>>width;
    cin>>height;
}

float Box::volume()
{
    return(lengh*width*height);
}

void Box::display()
{
    cout<<volume()<<endl;
}

int main()
{
    Box box1,box2,box3;
    box1.get_value();

    cout<<"volmue of bax1 is ";
    box1.display();
    box2.get_value();

    cout<<"volmue of bax2 is ";
    box2.display();
    box3.get_value();

    cout<<"volmue of bax3 is ";
    box3.display();

    return 0;
}

/*
please input lengh, width,height:1 2 3
volmue of bax1 is 6
please input lengh, width,height:2 3 4
volmue of bax2 is 24
please input lengh, width,height:3 4 5
volmue of bax3 is 60

Process returned 0 (0x0)   execution time : 7.267 s
Press any key to continue.

*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值