首页 > 试题广场 >

将下面程序分离类定义、类的实现和main函数,实现多文件程序

[问答题]
将下面程序分离类定义、类的实现和main函数,实现多文件程序结构:
//===================================
//ex0802.cpp
//使用 Cat类
//===================================
#include<iostream>
//-----------------------------------
class  Cat{
  int  itsAge;
public:
int  getAge();
void setAge(int age);
void meow();                      //喵喵叫
};//---------------------------------
int Cat::getAge(){  return itsAge;  }
void Cat::setAge(int age) {  itsAge=age;   } 
void Cat::meow() { std: :cout<<”Meow.\n”;  }
//-----------------------------------
int main(){ 
  Cat  frisky;
  frisky.setAge(5) ;
  frisky.meow();
  std::cout<<”frisky is  a cat who is  ”<<frisky.getAge()<<”  years  old.\n";
  frisky.meow();
}//==================================

推荐
使用类的应用部分:
//===================================
//ex0802.cpp
using Cat class
//===================================
#include"cat.h"
#include<iostream>
//-----------------------------------
int main(){ 
  Cat  frisky;
  frisky.setAge(5) ;
  frisky.meow();
  std::cout<<”frisky is  a cat who is  ”<<frisky.getAge()<<”  years  old.\n";
  frisky.meow();
}//==================================
类定义部分:
//===================================
//Cat.cpp
//===================================
#include"cat.h"
#include<iostream>
//-----------------------------------
int Cat::getAge(){  return itsAge;  }
void Cat::setAge(int age) { itsAge=age;  } 
void Cat::meow() { std: :cout<<”Meow.\n”;  }
//-----------------------------------
类实现部分:
//===================================
//Cat.h
//===================================
class  Cat{
  int  itsAge;
public:
int  getAge();
void setAge(int age);
void meow();                      //喵喵叫
};//---------------------------------

发表于 2018-04-18 20:34:49 回复(0)