C++ Primer Plus第六版编程练习12.1解答
来源:互联网 发布:中国出境游数据 编辑:程序博客网 时间:2024/06/05 15:04
Cow.h
#ifndef COW_H_#define COW_H_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
Cow.cpp
#include "Cow.h"#include <cstring>#include <iostream>using std::strcpy;using std::strlen;Cow::Cow(){ name[0]='\0'; hobby=new char [1]; //不能用new char,因为与析构函数不对应 hobby[0]='\0'; weight=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; return *this;}void Cow::ShowCow() const{ std::cout<<"name: "<<name<<std::endl; std::cout<<"hobby: "<<hobby<<std::endl; std::cout<<"weight: "<<weight<<std::endl; std::cout<<std::endl;}
main.cpp
#include "Cow.h"#include <iostream>int main(){ Cow c1; c1.ShowCow(); Cow c2("zhangzhiping", "basketball", 67); c2.ShowCow(); std::cout<<"Assign one object to another:\n"; c1=c2; c1.ShowCow(); std::cout<<"Initialize one object to another:\n"; Cow c3=c2; c3.ShowCow(); std::cout<<"Done!\n"; std::cin.get(); return 0;}
0 0
- C++ Primer Plus第六版编程练习12.1解答
- C++ Primer Plus第六版编程练习6.7解答
- C++ Primer Plus第六版编程练习6.9解答
- C++ Primer Plus第六版编程练习7.10解答
- C++ Primer Plus第六版编程练习8.2解答
- C++ Primer Plus第六版编程练习8.6解答
- C++ Primer Plus第六版编程练习9.1解答
- C++ Primer Plus第六版编程练习9.4解答
- C++ Primer Plus第六版编程练习10.3解答
- C++ Primer Plus第六版编程练习10.4解答
- C++ Primer Plus第六版编程练习10.5解答
- C++ Primer Plus第六版编程练习10.6解答
- C++ Primer Plus第六版编程练习10.8解答
- C++ Primer Plus第六版编程练习11.1解答
- C++ Primer Plus第六版编程练习11.2解答
- C++ Primer Plus第六版编程练习11.3解答
- C++ Primer Plus第六版编程练习11.4解答
- C++ Primer Plus第六版编程练习11.5解答
- 指针作为函数参数,申请内存
- EM算法嗅探
- Sencha Touch入门:Sencha Touch开发环境搭建及使用 Sencha Cmd 自动创建项目框架
- python整理笔记2(初学者需注意--自己曾遇到的问题)
- SqlServer 列数据比对 转载整理
- C++ Primer Plus第六版编程练习12.1解答
- poj 2516 最小费用最大流
- Leetcode第三题_Longest Substring Without Repeating Characters
- 【C语言】DigitSum(n) (递归函数)
- 小白也刷题-----leetcode去除数组中指定元素
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决
- 盘点有哪些国家自主研发自己的操作系统
- C++ Primer Plus第六版编程练习12.2解答
- Struts2中拦截器的配置