shiqiyu / cpp Goto Github PK
View Code? Open in Web Editor NEWLecture notes, projects and other materials for Course 'CS205 C/C++ Program Design' at Southern University of Science and Technology.
Lecture notes, projects and other materials for Course 'CS205 C/C++ Program Design' at Southern University of Science and Technology.
CPP/week12/examples/virtual.cpp
Lines 5 to 14 in 14c7055
CPP/week12/examples/virtual.cpp
Lines 49 to 51 in 14c7055
delete p;
is undefined behavior, so it might call Person::~Person only and leak the string id
.
In general, we should explicitly define a virtual
destructor in polymorphic base classes. See C.127: A class with a virtual function should have a virtual or protected destructor .
这是我在于老师2020秋季学C/C++程序设计的所有project和作业的代码开源链接,总评91分,为A-等级,供各位上于老师C/C++课的同学们参考:https://github.com/YeeTone/CS205-2020Fall
派生访问说明符拼写错了,应该是 public
而不是 pubic
。
CPP/week12/examples/derived-memory/mystring.hpp
Lines 76 to 80 in 14c7055
派生类的构造函数、拷贝构造函数和拷贝赋值运算符通常应该是 public
而不需要是 private
。
派生类的拷贝构造函数的规范实现应该是调用基类的拷贝构造函数来进行初始化派生类对象的基类部分,课件这里,调用基类的普通构造函数是不合适的,并且程序也是无法通过编译的,因为基类MyString的数据成员是 private
的,所以派生类MyMap是无法直接访问基类的 buf_len
和 characters
数据成员的。
派生类MyMap的拷贝构造函数的规范实现代码如下:
public:
MyMap(const MyMap& mm) : MyString(mm), keyname( /* initialize derived class members */)
{
// Do the rest of the copy constructor here...
}
派生类的拷贝控制成员的规范实现示例代码如下:
#include <iostream>
#include <string>
#include <utility>
class Base
{
// ...
public:
Base() = default;
Base(const Base&) = default;
Base(Base&&) = default;
Base& operator=(const Base&) = default;
Base& operator=(Base&&) = default;
virtual ~Base() = default;
// ...
};
class Derived : public Base
{
// ...
public:
Derived() = default;
Derived(const Derived& rhs) : Base{ rhs }, derived_{ rhs.derived_ }
{
// Do the rest of the copy constructor here...
}
// If the move constructor doesn't throw an exception, it should be marked noexcept.
Derived(Derived&& rhs) noexcept : Base{ std::move(rhs) }, derived_{ std::move(rhs.derived_) }
{
// Do the rest of the move constructor here...
}
Derived& operator=(const Derived& rhs)
{
// The copy assignment operator can gracefully handle self-assignment.
if (this != &rhs)
{
Base::operator=(rhs);
derived_ = rhs.derived_;
// Do the rest of the copy assignment operator here...
}
return *this;
}
// If the move assignment operator doesn't throw an exception, it should be marked noexcept.
Derived& operator=(Derived&& rhs) noexcept
{
// The move assignment operator can gracefully handle self-assignment.
if (this != &rhs)
{
Base::operator=(std::move(rhs));
derived_ = std::move(rhs.derived_);
// Do the rest of the move assignment operator here...
}
return *this;
}
~Derived() override = default;
private:
std::string derived_;
// ...
};
int main()
{
Derived d0, d1, d2;
Derived d3{ d0 };
Derived d4{ std::move(d0) };
d1 = d2;
d1 = std::move(d2);
return 0;
}
CPP/week11/examples/example2/mystring.hpp
Lines 29 to 48 in 8d45044
See: Assignment Operators, C++ FAQ (isocpp.org)
If x = x
, bad errors will occur.
We can handle self-assignment by explicitly testing for self-assignment:
MyString& operator=(const MyString& ms)
{
if (this != &ms)
{
create(ms.buf_len, ms.characters);
}
return *this;
}
In create
function, if new char[this->buf_len]{};
throws an exception, *this
won't keep a valid state. So, copy assignment operator is not exception safe.
The solution is copy the underlying data firstly, then delete *this
's old resource:
bool create(int buf_len, const char* data)
{
char* new_characters = NULL;
if (buf_len != 0)
{
new_characters = new char[buf_len] {};
}
if ((new_characters != NULL) && (data != NULL))
strncpy(new_characters, data, buf_len);
release();
this->buf_len = buf_len;
this->characters = new_characters;
return true;
}
See also
At week08/examples/matoperation.cpp
float sum[8]={0};
this may lead to discontinues memory, use
float *sum = static_cast<float*>(aligned_alloc(256, 8*sizeof(float)));
instead may be better?
Is there anything needed to be modified?
(Link: https://github.com/YeeTone/CS205-2020Fall)
请问于老师在2021 Fall的C/C++课程中,是否会安排final exam?(就问问,逃)
CPP/week11/examples/unique_ptr.cpp
Line 56 in 8d45044
课件这里,其实可以这样进行拷贝: std::unique_ptr<MyTime> mt3 = std::make_unique<MyTime>(*mt1);
虽然 std::unique_ptr
删除了 copy constructor 和 copy assignment operator ,但其实我们可以借助解引用操作变通地对 std::unique_ptr
进行拷贝。
deep copy 示例如下:
std::unique_ptr<std::string> up1(std::make_unique<std::string>("Good morning"));
// copy construct!
std::unique_ptr<std::string> up2(std::make_unique<std::string>(*up1));
// safe copy construct!
std::unique_ptr<std::string> up3(up1 ? std::make_unique<std::string>(*up1) : nullptr);
// copy assignment!
up2 = std::make_unique<std::string>(*up1);
// safe copy assignment!
up3 = up1 ? std::make_unique<std::string>(*up1) : nullptr;
其它的例证:
week10-example3-time.hpp 中调用的 std::to_string
未包含头文件 <string>
In my machine
$ uname -a
Linux DELL-Inspiron7580 5.10.16.3-microsoft-standard-WSL2 #1 SMP Fri Apr 2 22:23:49 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
init.cpp
output is
num1 = 0
num2 = 0
这一页底下的example标的是example1, 应该是example2 ?
感谢于老师带来精彩的课程。
一个小问题,为什么 lab 的内容与 lecture 内容不是对应的呢?比如说,week05 lecture 的内容是指针,但是 lab 却是 cmake
感谢于老师无私的提供了如此精致细致的c++课程。(应该要有不少老教授们copy and finetune了)
在如今大学课堂上往往学不到东西,大学教授往往糊弄了事的现状里,于老师真是一股清流,谢谢您。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.