C++中的class类的public属性和private属性的区别
Class成员组成
一个class类由public公有成员(实例化后的对象可直接访问和),private(需借助public函数进行访问)两种成员组成。
例如://C++代码
class A {
public:
int sum(int a, int b) {
int c = a + b;
return c;
}
int setweight(int c){
weight = c; //在private中已经定义过类型 直接访问即可
return weight;
}
private:
int weight;
};
main主函数调用类中函数方法
#include<iostream>
#include<stdio.h>
int main() {
int a,b,c;
A d; //实例化一个A类 对象 d
c = d.sum(a,b); //调用public函数
cout<< '计算后和值c ='<< c <<endl;
A e;//实例化一个A类对象 e
int weight = e.setweight (c)
cout<< 'weight is ' << weight <<endl;
return 0;
}