《面向對象基礎:C++實現》學習筆記之九

 1,P28
#include <iostream.h>
class Student
{
private :
 char Name[120];
 float TotalMarks;
 float NoOfExams;
 float Average;
 float CalcAverage();
public:
 void GetStats();
    void ShowStats();
};
float Student::CalcAverage()
{
 return(TotalMarks/NoOfExams);
}
void Student::ShowStats()
{
 cout<<"學生姓名:"<<Name<<endl;
 cout<<"總成績: "<<TotalMarks<<endl;
 cout<<"參加考試的數目:"<<NoOfExams<<endl;
 cout<<"每門考試的平均成績:"<<Average<<endl;
 cout<<endl;
}
void Student::GetStats()
{
    cout<<"姓名: ";
    cin>>Name;
 cout<<"總成績:";
 cin>>TotalMarks;
    cout<<"考試數目: ";
 cin>>NoOfExams;
 Average=CalcAverage();
}
int main(void)
{
 Student Kerry,Packer;
 Kerry.GetStats();
 Kerry.ShowStats();
 Packer.GetStats();
 Packer.ShowStats();
 return 0;
}

2,P31
#include <iostream.h>
#include <string.h>
class String
{
private:
 char *str;
public:
 String()
 {
  cout<<"調用了默認構造函數。";
 }
 String(char *s)   //構造函數
 {
  int length=strlen(s);
  str=new char[length+1];
  strcpy(str,s);
  cout<<"第二個構造函數";
 }
 ~String()  //析構函數
 {
  cout<<"調用了默認析構函數";
 }
 };
 int main()
 {
 String obj;
 String obj1("您好");
 return 0;
 }


3,p33
#include <iostream.h>
class alpha
{
private:
 static int count;  //靜態數據成員
public:
 alpha()   //析構函數增加count
 {
  count++;
 }
 static void display_count()  //靜態成員函數
 {
  cout<<count;
  cout<<endl;
 }
};
int alpha::count=0;
int main()
{
 alpha::display_count();  //創建任何對象之前
 alpha obj1,obj2,obj3;
 alpha::display_count();  //創建三個對象之後
 return 0;
}

4,P34
#include <iostream.h>
class Person
{
private:
 int age;
public:
 void display();
};
void Person::display()
{
this->age=25;    //與age=25一樣
cout<<this->age;  //與cout<<age一樣
cout<<endl;
};
int main()
{
 Person Jack;
 Jack.display();
 return 0;
}

5,P37
#include <Stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
class Student
{
private:
 int RollNum;
 char Name[26];
    float Marks[7];
 float Percent;
public:
 float GetDetails();
 float PrintDetails();
};
float Student::GetDetails()
{
 int ctr=1;
 float total=0;
 cout<<"充號:";
 cin>>RollNum;
 cout<<"姓名:";
 cin>>Name;
 cout<<"成績:";
 while(ctr<7)
 {
  cout<<"請輸入科目"<<ctr<<"獲得的成績:";
  cin>>Marks[ctr];
  total=total+Marks[ctr];
  ctr=ctr+1;
 }
 Percent=total/6;
 return(0);
}
float Student::PrintDetails()
{
 cout<<"序號:"<<RollNum;
 cout<<endl;
 cout<<"姓名:"<<Name;
 cout<<endl;
 cout<<"平均分:"<<Percent;
 cout<<endl;
 return(0);
}
int main()
{
 Student records[3];
 int x=0;
 int y=0;
 while (x<3)
 {
  records[x].GetDetails();
  x=x+1;
 }
 while(y<3)
 {
  records[y].PrintDetails();
  y=y+1;
 }
 return 0;
}


6,P38
#include <iostream.h>
#include <string.h>
class Employee
{
private:
 char Name[40];
 int Age;
 double Salary;
public:
 Employee(char *n,int a,double s); //構造函數
 void Display();
};
Employee::Employee(char *n,int a,double s)
{
 strcpy(Name,n);
 Age=a;
 Salary=s;
};
void Employee::Display()
{
 cout<<Name;
 cout<<endl;
 cout<<Age;
 cout<<endl;
 cout<<Salary;
 cout<<endl;
}
int main()
{
    Employee ManyEmps[3]=
 {
    Employee("Slaycke xulmbeng",33,96000),
    Employee("Peorje Posteau",45,350000),
    Employee("Teoberque Jailbird",50,110000)
 };
    int x=0;
 while(x<3)
 {
      ManyEmps[x].Display();
   x=x+1;
 }
 return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章