Maplewing

8/19 C++: Class

Aug 18th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /*
  5.     Struct版本:
  6.     struct Student{
  7.         char name[80];
  8.         int score;
  9.         int age;
  10.         int blood; // 1: A, 2: B, 3: AB, 4: O
  11.     }
  12.    
  13.     void printStudent(Student s){
  14.         printf("%s", s.name);
  15.         printf("(%d, ", s.age);
  16.         if( s.blood == 1 ){
  17.             printf("A");
  18.         }
  19.         else if( s.blood == 2 ){
  20.             printf("B");
  21.         }
  22.         else if( s.blood == 3 ){
  23.             printf("AB");
  24.         }
  25.         else if( s.blood == 4 ){
  26.             printf("O");
  27.         }
  28.         printf("): 成績: %d\n", s.score);
  29.     }
  30.  
  31. */
  32.  
  33. class Student{
  34.     public:
  35.         // 屬性、資料(變數部分)
  36.         char name[80];
  37.         int score;
  38.         int age;
  39.         int blood; // 1: A, 2: B, 3: AB, 4: O
  40.        
  41.         // 操作、函式、方法(函式部分)
  42.         void print(){
  43.             printf("%s", name);
  44.             printf("(%d, ", age);
  45.             if( blood == 1 ){
  46.                 printf("A");
  47.             }
  48.             else if( blood == 2 ){
  49.                 printf("B");
  50.             }
  51.             else if( blood == 3 ){
  52.                 printf("AB");
  53.             }
  54.             else if( blood == 4 ){
  55.                 printf("O");
  56.             }
  57.             printf("): 成績: %d\n", score);
  58.         }
  59.  
  60. };
  61.  
  62.  
  63. int main(){
  64.     /*
  65.         C: struct Student
  66.         C++: Student
  67.        
  68.         Struct 版本:
  69.         Student s1 = { "Sam", 85, 18, 1 };
  70.         Student s2 = { "Amy", 70, 18, 2 };
  71.        
  72.         printStudent(s1);
  73.         printStudent(s2);
  74.     */
  75.    
  76.     Student s1 = { "Sam", 85, 18, 1 };
  77.     Student s2 = { "Amy", 70, 18, 2 };
  78.    
  79.     s1.print();
  80.     s2.print();
  81.    
  82.    
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment