二、类和对象
1.类基础
class.cpp
#include <iostream>
using namespace std;
class Student{
public:
void setValue(char, int);
private:
char sex;
int age;
};
void Student::setValue(char sex, int age){
this->sex = sex;
this->age = age;
}
int main(int argc, char *argv[]){
class Student stu;
stu.setValue(1, 4);
class Student *stu1;
Student stu2;
Student *stu3;
stu1 = &stu;
stu3 = new Student;
stu1->setValue(2, 3);
return 0;
}
2.简单的学生类
studentClass.cpp
#include <iostream>
using namespace std;
class Student{
public:
void setValue(int, float);
int getAget();
float getWeight();
private:
int age;
float weight;
};
void Student::setValue(int age, float weight){
this->age = age;
this->weight = weight;
}
int Student::getAge(){
return age;
}
float Student::getWeight(){
return weight;
}
int main(int argc, char *argv[]){
Student stu;
stu.setValue(18, 120);
Student stu1;
stu1 = stu;
cout << stu1.getAge() << endl;
Student *p;
p = &stu;