【12.1】c++ primer plus 课后编程答案

来源:互联网 发布:天蝎网络第二季看不到 编辑:程序博客网 时间:2024/06/07 04:15
C++ PRIMER PLUS 课后答案 使用IDE为window7系统下的VS2010
/*user.h*/
#ifndef USERSH_H_#define USERSH_H_#include <string>using std::ostream;using std::istream;class Cow{char name[20];char * hobby;double weight;public:Cow();Cow(const char * nm,const char * ho,double wt);Cow(const Cow & c);~Cow();Cow & operator = (const Cow & c);void showcow()const;};#endif
/*userfucntion.cpp*/#include "usersh.h"#include <iostream>#include <cstring>using std::cout;using std::cin;using std::ostream;using std::istream;Cow::Cow(){name[0]='\0';weight=50;hobby=new char[1];hobby[0]='\0';}Cow::Cow(const char * nm,const char * ho,double wt){strcpy(name,nm);hobby=new char [strlen(ho)+1];strcpy(hobby,ho);weight=wt;}Cow::Cow(const Cow & c){strcpy(name,c.name);hobby=new char [strlen(c.hobby)+1];strcpy(hobby,c.hobby);weight=c.weight;}Cow::~Cow(){delete [] hobby;}Cow & Cow::operator =(const Cow & c){if(this==&c)return * this;delete [] hobby;strcpy(name,c.name);hobby=new char [strlen(c.hobby)+1];strcpy(hobby,c.hobby);weight=c.weight;}void Cow::showcow()const{cout<<"name:"<<name<<'\n';cout<<"hobby:"<<hobby<<'\n';cout<<"weight:"<<weight<<'\n';}
/*main*/#include <iostream>#include <Windows.h>#include "usersh.h"using std::cout;using std::cin;using std::endl;int main(){   Cow one("xiao ming","sleep ",40);one.showcow();Cow two(one);two.showcow();Cow three;three=two;three.showcow();system("pause");return 0;}



原创粉丝点击