第二章 面向对象的基本概念之——访问对象的成员

1、对象访问成员

使用对象访问成员,通过对象访问的一般格式:
对象名.成员变量名
对象名.成员函数名(参数)

要注意:成员的访问权限
私有成员在类的外部不可以直接调用
【示例】

void Student::printStudent () const
{
	cout <<"姓名:"<< name <<"\t 生日:";
	birthday.printDate () ;
	cout << endl:
};

int main ()
{
	Student ss ;
	int y,m,d;
	string name_;
	cout <<"请输入学生的姓名和生日,生日以\"年 月 日\"的次序输入:";
	cin >> name_>> y >> m >> d;
	ss.setStudent (name_,myDate(y,m,d)) ;
	ss.printStudent () ;
	return 0;
}

【运行结果】

学生的姓名和生日,生日以”年 月 日”的次序输入:李四 1998 7 9
姓名:李四	生日: 1998/7/9

2、指针访问,

使用指针访问,格式为:
指针->成员名
【示例】

void Student::printstudent () const
{
	cout <<"姓名:" << name << "\t 生日:";
	birthday.printDate () ;
	cout << endl;
}
int main()
{
	Student ss;
	int y,m,d;
	string name ;
	*sp = &ss; //sp是对象指针,指向对象ss的地址
	cout <<"请输入学生的姓名和生日,生日以\"年 月 日\"的次序输入:";
	cin >> name >> y >> m >> d;
	sp->setStudent (name.mypate(ym,d)) ;
	sp->printStudent () ;
	ss.printStudent();
	return 0;
}

【运行结果】

请输入学生的姓名和生日,生日以"年 月 日”的次序输入:张三 1998 7
姓名:张三	 	生日:1998/7/9
姓名:张三	 	生日:1998/7/9

3、引用访问

使用引用访问,格式为:
引用名.成员名
【示例】

Student ::printStudent () const
{
	cout <<"姓名:" << name << "\t 生日:";
	birthday.printDate () ;
	cout << end1:
}

int main()
{
	Student ss ;
	int y,m,d;
	string name ;
	student &sy = ss; //sy是ss的别名
	cout<<"请输入学生的姓名和生日,生日以\"年 月 日\"的次序输入:";
	cin >> name  >> Y >> m >> d;
	sy.setStudent (name ,myDate(y,m,d)) ;
	sy.printStudent () ;
	ss.printStudent () ;
	return 0;
}

4、例题

例题1:【程序分析题】 以下程序有一处错误,请指出并改正

#include<iostream.h>
class Test
{
	int x, y;
	public:
		fun(int l, int j) {x=l;y=j;}
		show(){
		cout<<"x="<<x;
		if(y) coutt<<",y="<<y<<endl ;
		cout<<endl; 
		};
}
		
void main() 
{
	Test a;
	a.fun (1);
	show() ;
	a.fun (2, 4) ;
	a.showk() ;
}

【正确答案】: 【a.fun (1); 】改为: 【a.fun (1,2);】
【答案解析】:只有一个参数,除非声明部分定义一个默认值

例题2:【程序设计题】下面是一个类的测试程序,请设计出能使用如下测试程序的类。

int main () {
	Test a;
	a. init(2019, 100) ; 
	a. print () ;
	return 0; 
}

其执行结果为:
测试结果为: 2019-100=1949
【正确答案】

class Test
{
	int x, y;
	public:
		void init(int a, int b) {x=a;y=b;}
		void print() {cout<<"测试结果为:"<< x <<"-"<< y <<"="<< x-y <<endl;
}

例题3:【程序设计题】类book,含有2个数据成员num编号和price价格,有成员函数set和display分别输入和显示书的编号和价格,在main中调用所有成员函数
#include <iostream>
using namespace std ;
class book
{
	private :
		char num[8 ]
		______(1)_________ 
	public:
		void set()
			{ cin>> num>> price }; 
		void display( )
			{cout<< num << " "<< price<< endl; }
};

void main ()
{
	book b;
	______(2)______
	b. display();
}
【正确答案】:
(1) double price
(2) b.set();

例题4:【选择题】对于下面定义的类Myclass,在函数 f( )中将对象成员n的值修改为50的语句应该是 ()
class Myclass {
	public:
		Myclass (int i) {n=i;}
		void SetNum(int x) {n=x;}
	private:
		int n;
};

int f() 
{
	Myclass *p =new Myclass (45);
	______________________
}

A. p->SetNum(50);
B. p->n = 50;
C. SetNum(50);
D.*p-> SetNum (50) ;

【正确答案】: A
【答案解析】:n是私有的,不能直接赋值,指针->成员函数(参数)

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

润小仙女

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值