不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
C++的类
编辑:dnawo 日期:2010-11-15
C++类示例(cls.h):
调用示例:
与C#类比较,C++的类有如下特点:
·公有成员和私有成员明显分开;
·类属性没有set和get访问器;
·公有成员允许在类定义的外面进行定义;
·类结束需要一个分号;
·inline关键字表示该函数是一个内联函数;
·const关键字表示该成员函数内不会修改任何成员变量;
·创建类实例时不需要使用new关键字;
复制内容到剪贴板
程序代码

#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string _name;
int _age;
public:
//构造函数
Student()
{
_name = "";
_age = 0;
}
Student(string name, int age)
{
_name = name;
_age = age;
}
//析构函数
~Student()
{
_name = "";
_age = 0;
}
//公有方法
void SetName(string name){_name=name;}
string GetName()const{return _name;}
void SetAge(int);
int GetAge()const;
};
//在类外定义方法
inline void Student::SetAge(int age)
{
_age = age;
}
inline int Student::GetAge()const
{
return _age;
}
#include <string>
using namespace std;
class Student
{
private:
string _name;
int _age;
public:
//构造函数
Student()
{
_name = "";
_age = 0;
}
Student(string name, int age)
{
_name = name;
_age = age;
}
//析构函数
~Student()
{
_name = "";
_age = 0;
}
//公有方法
void SetName(string name){_name=name;}
string GetName()const{return _name;}
void SetAge(int);
int GetAge()const;
};
//在类外定义方法
inline void Student::SetAge(int age)
{
_age = age;
}
inline int Student::GetAge()const
{
return _age;
}
调用示例:
复制内容到剪贴板
程序代码

#include <iostream>
#include "cls.h"
using namespace std;
int main()
{
Student stu("zhang",29);
cout<<"Name:"<<stu.GetName()<<endl;
cout<<"Age:"<<stu.GetAge()<<endl;
return 0;
}
#include "cls.h"
using namespace std;
int main()
{
Student stu("zhang",29);
cout<<"Name:"<<stu.GetName()<<endl;
cout<<"Age:"<<stu.GetAge()<<endl;
return 0;
}
与C#类比较,C++的类有如下特点:
·公有成员和私有成员明显分开;
·类属性没有set和get访问器;
·公有成员允许在类定义的外面进行定义;
·类结束需要一个分号;
·inline关键字表示该函数是一个内联函数;
·const关键字表示该成员函数内不会修改任何成员变量;
·创建类实例时不需要使用new关键字;
评论: 0 | 引用: 0 | 查看次数: 3594
发表评论
请登录后再发表评论!