《面向对象基础: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;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值