【C++】点和箭头用法区别

.用于访问成员对象

->用于访问成员对象指针

main.cpp

#include <iostream>
#include "score.h"
using namespace std;
int main()
{
	score m;
	score *p = new score;
	m.set_name("xiaoming");
	m.set_age(5);
	p->set_name("limign");
	p->set_age(5);
	cout << m.get_name() << endl;
	cout << m.get_age() << endl;
	cout << p->get_name() << endl;
	cout << p->get_age() << endl;
	system("pause");
	return 0;
}

score.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
class score
{
private:
	char name[100];
	int age;
public:
	score();
	~score();
	void set_name(const char *s);
	void set_age(const int i);
	const char *get_name() const;
	const int get_age() const;
};

score.cpp

#include "score.h"
#include <string.h>
 
 
score::score()
{
}
 
 
score::~score()
{
}
 
void score::set_name(const char *s)
{
	strcpy(this->name, s);
}
 
void score::set_age(const int i)
{
	this->age = i;
}
 
const char *score::get_name() const
{
 
	return name;
}
 
const int score::get_age() const
{
	return age;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值