C++Primer Plus 第12章 类和动态内存分配 12.10编程练习第1题
` 提示:练习一定要动手操作
涉及标准函数及关键词
1,new[],delete[],strlen(),strcpy_s(),cout,endl,explicit
例如:1,对于下面的类的声明:
`提示:设计数组和字符串的new,delete
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
例如:C++动态内存的分配和类的使用练习。
提示:以下是本篇文章正文内容,下面案例可供参考
一、New Delete
说明:new,delete是C++动态内存申请的方法
该方法必须成对出现。如果是new,必须有对应的delete
如果是new[] ,则必须有对应的delete[]
二、使用步骤
1.构造函数中使用New
代码如下(示例):
Cow::Cow(const char* name, const char* hobby, double weight):weight(weight)
{
unsigned strLen = strlen(name) < 20 - 1 ? strlen(name) : 20 - 1;
strcpy_s(this->name, name);
this->name[strLen] = '\0';
this->hobby = new char[strlen(hobby) + 1];
strcpy_s(this->hobby, strlen(hobby) + 1, hobby);
}
Cow::Cow(const Cow& cow):weight(cow.weight)
{
strcpy_s(name, cow.name);
hobby = new char[strlen(cow.hobby) + 1];
strcpy_s(hobby, strlen(cow.hobby) + 1, cow.hobby);
}
2 同时重载运算符中的new
Cow&
Cow::operator=(const Cow& cow)
{
if (&cow == this)
{
return *this;
}
strcpy_s(name , cow.name);
delete[]hobby;
hobby = new char[strlen(cow.hobby) + 1];
strcpy_s(hobby, strlen(cow.hobby) + 1, cow.hobby);
weight = cow.weight;
return *this;
}
3.析构函数中delete
代码如下(示例):
Cow::~Cow()
{
delete[]hobby;
}
4 本题的参考作业
1 头文件的声明
#ifndef PE12_1_H_
#define PE12_1_H_
class Cow
{
private:
char name[20]; //奶牛名称
char* hobby; //奶牛爱好
double weight; //奶牛体重
public:
//Cow();
//在C++中,explicit关键字用来修饰类的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换
explicit Cow(const char* name = " ", const char* hobby = " ", double weight = 0);
Cow(const Cow& cow);
~Cow();
Cow& operator=(const Cow& cow);
void ShowCow()const;//display all cow data
};
#endif // !PE12_1_H_
2 方法的实现
//pe12_1.cpp -- methoms
#include<iostream>
#include<cstring>
#include"pe12_1.h"
Cow::Cow(const char* name, const char* hobby, double weight):weight(weight)
{
unsigned strLen = strlen(name) < 20 - 1 ? strlen(name) : 20 - 1;
strcpy_s(this->name, name);
this->name[strLen] = '\0';
this->hobby = new char[strlen(hobby) + 1];
strcpy_s(this->hobby, strlen(hobby) + 1, hobby);
}
Cow::Cow(const Cow& cow):weight(cow.weight)
{
strcpy_s(name, cow.name);
hobby = new char[strlen(cow.hobby) + 1];
strcpy_s(hobby, strlen(cow.hobby) + 1, cow.hobby);
}
Cow::~Cow()
{
delete[]hobby;
}
Cow&
Cow::operator=(const Cow& cow)
{
if (&cow == this)
{
return *this;
}
strcpy_s(name , cow.name);
delete[]hobby;
hobby = new char[strlen(cow.hobby) + 1];
strcpy_s(hobby, strlen(cow.hobby) + 1, cow.hobby);
weight = cow.weight;
return *this;
}
void
Cow::ShowCow()const
{
std::cout << name << "\t" << hobby << "\t" << weight;
}
//pe12_2.cpp
3 测试函数
#pragma region 练习1 测试函数
/*
测试函数是题目给定的。为了方便我也copy过来了。
*/
#if 1
#include <iostream>
#include"pe12_1.h"
int main()
{
using namespace std;
Cow cow1("cow1", "eat", 700.2);
cout << "cow1: ";
cow1.ShowCow();
cout << endl << endl;
Cow cow2 ;
cout << "cow2: ";
cow2.ShowCow();
cout << endl << endl;
Cow cow3(cow1);
cout << "cow3: ";
cow3.ShowCow();
cout << endl << endl;
Cow cow4;
cow4 = cow1;
cout << "cow4: ";
cow4.ShowCow();
cout << endl << endl;
cout << endl;
return 0;
}
#endif
#pragma endregion
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了new,delete的使用,而new,delete提供了大量能使我们快速便捷地动态申请内存的方法。
标签:12,weight,cow,Cow,C++,Plus,hobby,strlen,name From: https://blog.csdn.net/zhyjhacker/article/details/139569923