面向对象程序设计7-11

7
#include <iostream.h>
class STUDENT{
  int total;
  public:
    int get_score();
    void display();
    static void sort(STUDENT *s[3]);
  STUDENT(int t){
    total=t;
  }
};
int STUDENT::get_score()
{
  return total;
}
void STUDENT::display()
{
  cout<<total<<endl;
}
void STUDENT::sort(STUDENT* s[3])
{
  STUDENT * t;
  for(int i=0;i<3;i++)
  {
    for(int j=2;j>i;j--)
    {
      if(s[j]->get_score() > s[j-1]->get_score())
      {
    t=s[j];
    s[j]=s[j-1];
    s[j-1]=t;
      }
    }
  }
  for(int k=0;k<3;k++)
    cout<<s[k]->get_score()<<endl;
}
void main()
{
  int s1,s2,s3;
  cout<<"please input three score:"<<endl;
  cin>>s1>>s2>>s3;
  STUDENT *s[2];
  s[0]=new STUDENT(s1);
  s[1]=new STUDENT(s2);
  s[2]=new STUDENT(s3);
  cout<<"The result is:"<<endl;
  STUDENT::sort(s);
}

8#include <iostream.h>
const int size=8;
class stack{
  private:
    char stck[size];
    int tos;
  public:
    void push(char ch);
    char pop();
    stack(){
      tos=0;
    }
};
void stack::push(char ch)
{
  if(tos==size)
  {
    cout<<"The stack is full now!"<<endl;
   // return;
    }
   stck[tos++]=ch;
}
char stack::pop()
{
  if(tos==0)
  {
   // cout<<"The stack is empty now!"<<endl;
    return 0;
    }
    tos--;
  return stck[tos];
}
void main()
{
  stack t;
  char c;
  t.push('5');
  t.push('2');
  t.push('6');
  t.push('7');
  t.push('3');
  while((c=t.pop())!=0)
  {
    cout<<c<<endl;
  }
}

9
#include <iostream.h>
class person{
  private :
    char *name;
    char *no;
  public:
   void setName(char *c)
   {
     name=c;
   }
   void setNo(char *n)
   {
     no=n;
   }
   char *getName()
   {
     return name;
   }
   char *getNo()
   {
     return no;
   }
} ;
class student:public person{
  private:
    char *cla;
  public:
    void setClass(char *cl)
    {
      cla=cl;
    }
    char *getClass()
    {
      return cla;
    }
};
class teacher:public person{
  private:
    char *dept;
  public:
    void setDept(char *de)
    {
      dept=de;
    }
    char *getDept()
    {
      return dept;
    }
};
void main()
{

}

10
#include <fstream.h>
#include <iostream.h>
void doLine(istream& fin,ostream& out);
void main(int argc,char **argv)
{
  if(argc!=3)
  {
    cout<<"command:xx.exe infilename.txt outfilename.txt"<<endl;
    return;
   }
  ifstream fin(argv[1]);
  ofstream out(argv[2]);
  if(!fin)
  {
    cout<<"file not found."<<endl;
    return;
  }
  char ch;
  while(fin.get(ch))
  {
    if(ch=='/')
    {
      fin.get(ch);
      if(!fin)
    break;
      if(ch=='/')
    doLine(fin,out);
      else
      {
    out.put('/');
    out.put(ch);
    }
    }
    else
      out.put(ch);
  }
}
void doLine(istream& fin,ostream& out)
{
  char ch;
  while(fin.get(ch))
    if(ch=='/n')
    {
      out.put('/n');
      return;
    }
    cout<<"Eof"<<endl;
}

11#include <iostream.h>
class Test{
  private:
    int num;
  public:
    void setNum(int n)
    {
      num=n;
    }
    int getNum()
    {
      return num;
    }
    Test(int i){
      num=i;
    }
};
  Test t[9]={0,0,0,0,0,0,0,0,0};
void main()
{
  int temp;
  for(int j=0;j<9;j++)
  {
    t[j].setNum(0X7FF);
    temp=t[j].getNum();
    cout<<temp<<endl;
    }
  delete [] t;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值