准备

1.在VS中新建一个项目-Viscal C++ ---常规--空项目

2.建立一个.h的头文件 定义一个类 声明其成员(C#中的属性和方法)

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
void setId(int id);
int getId();
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
int _id;
string _name;
int _age;
};

建立一个.cpp的文件  声明一个类 实现成员变量初始化操作

#include "Per.h";
using namespace std;
void Person::setId(int id){
this->_id = id;
}
void Person::setName(string name){
this->_name = name;
}
void Person::setAge(int age){
this->_age= age;
}
int Person::getId(){
return this->_id;
}
string Person::getName(){
return this->_name;
}
int Person::getAge(){
return this->_age;
}
int main(){
}

通过对象方式

Person Per;
Per.setId();
Per.setAge();
Per.setName("Tony");
int id = Per.getId();
string name = Per.getName();
int age = Per.getAge();
cout << id <<","<< name <<","<< age<<endl;
system("pause");
return ;

通过指针方式

Person *Per = new Person();
Per->setId(1);
Per->setName("Tommy");
Per->setAge(20);
int id = Per->getId();
string name = Per->getName();
int age = Per->getAge();
cout << id <<","<< name <<","<< age<<endl;
delete Per;
system("pause");
return 0;

完成代码例子

#include "Per.h";
using namespace std;
void Person::setId(int id){
this->_id = id;
}
void Person::setName(string name){
this->_name = name;
}
void Person::setAge(int age){
this->_age= age;
}
int Person::getId(){
return this->_id;
}
string Person::getName(){
return this->_name;
}
int Person::getAge(){
return this->_age;
}
int main(){
//1.通过对象方式访问
//Person Per;
//Per.setId(1);
//Per.setAge(25);
//Per.setName("Tony");
//int id = Per.getId();
//string name = Per.getName();
//int age = Per.getAge();
//2.通过指针方式访问
Person *Per = new Person();
Per->setId();
Per->setName("Tommy");
Per->setAge();
int id = Per->getId();
string name = Per->getName();
int age = Per->getAge();
cout << id <<","<< name <<","<< age<<endl;
delete Per;
system("pause");
return ;
}
 

C++ 通过对象方式 、指针方式两种方式去访问成员变量(属性或者方法)的更多相关文章

  1. 将图片base64格式转换为file对象并读取(两种方式读取)

    两种方式读取,一种URL.createObjectURL,另一种fileReader   var base64 = ` data:image/jpeg;base64,/9j/4AAQSkZJRgABA ...

  2. js对象取值的两种方式

    :"李四"}; var v1 = obj.name1; //张三, 使用点的方式 //报错,不能使用点的方式 ]; //李四,使用中括号的方式 var key = "na ...

  3. spring配置文件注解方式引入的两种方式

    一.#{beanID['propertiesName']}方式 <bean id="propertyConfigurer" class="org.springfra ...

  4. spring boot 集成 Filter 的两种方式

    两种方式:(两种方式同时存在时,@Bean优先@ServletComponentScan实例化,生成两个对象) 1)@ServletComponentScan注解+@WebFilter注解 2)@Be ...

  5. spring boot集成 servlet自动注册的两种方式

    有两种方式:(两种方式同时存在时,@Bean优先@ServletComponentScan实例化,生成两个对象) 1)通过@ServletComponentScan类注解 扫描 带有@WebServl ...

  6. .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格

    一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...

  7. Asp.net Web API 返回Json对象的两种方式

    这两种方式都是以HttpResponseMessage的形式返回, 方式一:以字符串的形式 var content = new StringContent("{\"FileName ...

  8. 对于String对象,可以使用"="赋值,也可以使用"new"关键字赋值,两种方式有什么区别?

    当你看见这个标题的时候,你可能会下意识的去想一下,这两种方式到底有什么样的区别呢? 且看下面的demo,自然便区分开了 /** * */ package com.b510.test; /** * Pr ...

  9. java 的对象拷贝(有深浅拷贝两种方式,深拷贝实现的两种方式(逐层实现cloneable接口,序列化的方式来实现))

    Java提高篇--对象克隆(复制)(转自:http://www.cnblogs.com/Qian123/p/5710533.html#_label0)   阅读目录 为什么要克隆? 如何实现克隆 浅克 ...

随机推荐

  1. 高性能CSS(二)

    避免CSS表达式 CSS表达式是动态设置CSS属性的强大(但危险)方法.Internet Explorer从第5个版本开始支持CSS表达式.下面的例子中,使用CSS表达式可以实现隔一个小时切换一次背景 ...

  2. hibernate的id生成策略

    欢迎转载,请注明出处http://www.cnblogs.com/shizhongtao/p/3436523.html 一.xml配置方式的id生成 <id name="id" ...

  3. UvaLive7362 Fare(欧拉函数)

    题意:求1~n的素因子之和. 分析:欧拉函数 #include<cstdio> #include<cstring> #include<cctype> #includ ...

  4. Linux 下面对物理地址的访问

    参考链接:http://zhuhaibobb.blog.163.com/blog/static/2744006720101049030606/ Linux内核提供了/dev/mem驱动,提供了一种直接 ...

  5. excl剔除不合格数据求平均值

    excl剔除不合格数据求平均值 trimmean函数 正态分布: CONFIDENCE.NORM 函数

  6. [UNIX环境高级编程](第三版)中apue.h的问题

    编译 gcc -g myls.c 时,报错 ‘找不到头文件 apue.h’ apue.h是作者自己写的一个文件,系统不自带.其中包含了常用的头文件,以及出错处理函数的定义. 需要到 http://ww ...

  7. MySQL和Navicat使用总结

    1.给字段设置默认字符值 ALTER TABLE `v_users`MODIFY COLUMN `picture` varchar(50) CHARACTER SET utf8 COLLATE utf ...

  8. vhost设定

    vhost设定   http.conf <Directory /> AllowOverride none #Require all denied </Directory>   ...

  9. Linux 查看某个用户的进程

    Linux 查看某个用户的进程 To view only the processes owned by a specific user, use the following command: top ...

  10. 实现Linux select IO复用C/S服务器代码

    已在ubuntu 下验证可用 服务器端 #include<stdio.h>#include<unistd.h>#include<stdlib.h>#include& ...