如何用CodeBlocks分多个文件编写一个C++程序
1个回答
展开全部
//建立一个空工程,将以下3个文件添加进去就行了。
//Cat.h文件(头文件)
#ifndef Cat_Header
#define Cat_Header
#include<iostream>
using namespace std;
namespace My
{
class Cat
{
public:
Cat(string name);
virtual~Cat();
void SetName(string name);
string GetName() const;
virtual void mew() const;
private:
string m_name;
};
}
#endif
//Cat.cpp文件(源文件)
#include"Cat.h"
My::Cat::Cat(string name)
{
this->m_name=name;
}
My::Cat::~Cat()
{
}
void My::Cat::SetName(string name)
{
this->m_name=name;
}
string My::Cat::GetName() const
{
return this->m_name;
}
void My::Cat::mew() const
{
cout<<this->m_name<<":"<<"喵喵。。。"<<endl<<this->m_name<<":主人,我饿了!"<<endl;
}
//Main.cpp文件(源文件)
#include"Cat.h"
using namespace My;
class PersianCat : public Cat
{
public:
PersianCat(string name);
virtual void mew() const;
};
PersianCat::PersianCat(string name):Cat(name)
{
}
void PersianCat::mew() const
{
Cat::mew();
cout<<this->GetName()<<":我要吃鱼!"<<endl<<endl;
}
int main(int argc,char *argv[])
{
Cat c("小猫猫");
PersianCat p("小波斯");
Cat &refcat=p;
Cat *ptcat=&p;
c.mew();
cout<<endl;
refcat.mew();
ptcat->mew();
system("PAUSE");
return EXIT_SUCCESS;
}
//Cat.h文件(头文件)
#ifndef Cat_Header
#define Cat_Header
#include<iostream>
using namespace std;
namespace My
{
class Cat
{
public:
Cat(string name);
virtual~Cat();
void SetName(string name);
string GetName() const;
virtual void mew() const;
private:
string m_name;
};
}
#endif
//Cat.cpp文件(源文件)
#include"Cat.h"
My::Cat::Cat(string name)
{
this->m_name=name;
}
My::Cat::~Cat()
{
}
void My::Cat::SetName(string name)
{
this->m_name=name;
}
string My::Cat::GetName() const
{
return this->m_name;
}
void My::Cat::mew() const
{
cout<<this->m_name<<":"<<"喵喵。。。"<<endl<<this->m_name<<":主人,我饿了!"<<endl;
}
//Main.cpp文件(源文件)
#include"Cat.h"
using namespace My;
class PersianCat : public Cat
{
public:
PersianCat(string name);
virtual void mew() const;
};
PersianCat::PersianCat(string name):Cat(name)
{
}
void PersianCat::mew() const
{
Cat::mew();
cout<<this->GetName()<<":我要吃鱼!"<<endl<<endl;
}
int main(int argc,char *argv[])
{
Cat c("小猫猫");
PersianCat p("小波斯");
Cat &refcat=p;
Cat *ptcat=&p;
c.mew();
cout<<endl;
refcat.mew();
ptcat->mew();
system("PAUSE");
return EXIT_SUCCESS;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询