Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- /*
- Struct版本:
- struct Student{
- char name[80];
- int score;
- int age;
- int blood; // 1: A, 2: B, 3: AB, 4: O
- }
- void printStudent(Student s){
- printf("%s", s.name);
- printf("(%d, ", s.age);
- if( s.blood == 1 ){
- printf("A");
- }
- else if( s.blood == 2 ){
- printf("B");
- }
- else if( s.blood == 3 ){
- printf("AB");
- }
- else if( s.blood == 4 ){
- printf("O");
- }
- printf("): 成績: %d\n", s.score);
- }
- */
- class Student{
- public:
- // 屬性、資料(變數部分)
- char name[80];
- int score;
- int age;
- int blood; // 1: A, 2: B, 3: AB, 4: O
- // 操作、函式、方法(函式部分)
- void print(){
- printf("%s", name);
- printf("(%d, ", age);
- if( blood == 1 ){
- printf("A");
- }
- else if( blood == 2 ){
- printf("B");
- }
- else if( blood == 3 ){
- printf("AB");
- }
- else if( blood == 4 ){
- printf("O");
- }
- printf("): 成績: %d\n", score);
- }
- };
- int main(){
- /*
- C: struct Student
- C++: Student
- Struct 版本:
- Student s1 = { "Sam", 85, 18, 1 };
- Student s2 = { "Amy", 70, 18, 2 };
- printStudent(s1);
- printStudent(s2);
- */
- Student s1 = { "Sam", 85, 18, 1 };
- Student s2 = { "Amy", 70, 18, 2 };
- s1.print();
- s2.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment