Visual studio中类的正确声明方式
第一步:右键单击项目—添加—类
第二步:点击“类”选项,出现如下复选框:
第三步:添加类名(仅创建一个简单类)
第四步:出现一个Cstudent.h文件与Cstudent.cpp文件
Cstudent.h文件是用来声明的,Cstudent.cpp是用来定义类中成员函数体的。实例如下:
① Cstudent.cpp文件:
#include "Cstudent.h"
#include<iostream>
using namespace std;
void Cstudent::input_value(int Dmark, string Dname, float Dheight)
{
mark = Dmark;
name = Dname;
height = Dheight;
}
void Cstudent::output_height()
{
cout << height << endl;
}
② Cstudent.h文件
#pragma once
#include <string>
class Cstudent
{
private:
int mark;
string name;
float height;
public:
void input_value(int Dmark, string Dname, float Dheight); // 成员函数声明
void output_height();
};
③ 主程序文件:
#include <iostream>
using namespace std;
#include"Cstudent.h"
int main()
{
Cstudent.input_value(87, "超级霸霸强", 185.6);
Cstudent.output_height();
}