c++构造的理解

来源:互联网 发布:windows syslog 编辑:程序博客网 时间:2024/06/01 22:39
#include<stdio
int main

int a,b,c,p,*p1,*p2,*p3;
printf("输入3位数整数");
#include<iostream>
#include<string>
using namespace std;




class Student{
public:            //公开数据
void display(){
cout << "num" << num << endl;
cout << "name" << name << endl;
cout << "sex" << sex << endl;
}
void set_time();  //函数声明
void show_time();
private:    //私有成员数据
int num;
string name;
char sex;
};
void Student::set_time(){
cin >> num >> name >> sex;    //给数据成员赋值
};
void Student::show_time(){               //输出数据成员
cout << num << name << sex << endl;




}
int main(){
Student stu;


/*stu.num = 50;                //这个是错误的,因为它的成员数据是私有的,在类外是不可以给里面的成员赋值,想要赋值必须调用里面公开的成员函数进行赋值
stu.name = "老王";
stu.sex = '男';*/


stu.set_time();            //调用里面的成员函数,进行赋值
//stu.show_time();
stu.display();                //调用里面的函数,进行输出
}

0 0
原创粉丝点击