#include <iostream>
using namespace std;
class Student {
public:
int age;
char name[20];
char address[20];
public:
void sayHello() {
cout<<"大家好1"<<endl;
}
int add(int a,int b){
int r =a+b;
return 0;
}
};
Student.cpp
#include <iostream>
#include <string.h>
#include "Student.h"
using namespace std;
int main() {
//使用类的第一种方法
//类名 对象名
Student zhangsan;
zhangsan.age = 20;
strcpy(zhangsan.name,"zhangsan");
strcpy(zhangsan.address,"huxian");
cout<<"zhangsan.age = "<<zhangsan.age<<endl;
cout<<"zhangsan.name = "<<zhangsan.name<<endl;
cout<<"zhangsan.address = "<<zhangsan.address<<endl;
zhangsan.sayHello();
int r = zhangsan.add(1,1);
return 0;
}
定义类的第二种方法
Student2.h文件
#include <iostream>
using namespace std;
class Student2 {
public:
int age;
char name[20];
char address[20];
public:
void sayHello();
int add(int a, int b);
};
void Student2::sayHello() {
cout<<"hello"<<endl;
}
int Student2::int add(int a, int b){
int r = a+b;
return r;
}
Student2.cpp
#include <iostream>
#include <string.h>
#include "Student2.h"
using namespace std;
int main() {
//使用类的第一种方法
//类名 对象名
Student2.zhangsan;
zhangsan.age = 20;
strcpy(zhangsan.name,"zhangsan");
strcpy(zhangsan.address,"huxian");
cout<<"zhangsan.age = "<<zhangsan.age<<endl;
cout<<"zhangsan.name = "<<zhangsan.name<<endl;
cout<<"zhangsan.address = "<<zhangsan.address<<endl;
zhangsan.sayHello();
int r = zhangsan.add(1,1);
return 0;
}
第三种方法
文件Student3.h
include <iostream>
using namespace std;
class Student3 {
public:
int age;
char name[20];
char address[20];
public:
void sayHello();
int add(int a, int b);
};
Student3.cpp
#include <iostream>
#include "Student3.h"
using namespace std;
void Student3::sayHello() {
cout<<"hello"<<endl;
}
int Student3::add(int a, int b){
int r = a+b;
return r;
}