实验1 类和对象(1)
#include<iostream>
using std::cout;
using std::endl;
class Point{
public:
Point(int x0 = 0, int y0 = 0);
Point(const Point &p);
~Point() = default;
int get_x() const { return x;};
int get_y() const { return y;};
void show() const;
private:
int x, y;
};
Point::Point(int x0, int y0): x{x0} , y{y0}{
cout << "constructor called." << endl;
}
Point::Point(const Point & p) : x{p.x}, y{p.y}{
cout << "copy constructor called." << endl;
}
void Point::show() const {
cout << "(" << x << "," << y << ")" << endl;
}
int main(){
Point p1(4, 5);
p1.show();
Point p2 = p1;
p2.show();
Point p3{p2};
p3.show();
cout << p3.get_x() << endl;
}
实验任务3
#include<iostream> #include<iomanip> using std::cout; using std::endl; class Clock{ public: Clock(int h = 0, int m = 0, int s = 0); Clock(const Clock & t); ~Clock() = default; void set_time(int h, int m = 0, int s = 0); void show_time() const; private: int hour, minute, second; }; Clock::Clock(int h, int m, int s): hour(h), minute{m}, second{s}{ cout << "constructor called" << endl; } Clock::Clock(const Clock & t): hour(t.hour), minute{t.minute}, second{t.second}{ cout << "copy constructor called" << endl; } void Clock::set_time(int h, int m, int s){ hour = h; minute = m; second = s; } void Clock::show_time() const { using std::setw; using std::setfill; cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second << endl; } Clock reset(){ return Clock(0, 0, 0); } int main(){ Clock c1(12, 0, 5); c1.show_time(); c1 = reset(); c1.show_time(); Clock c2(c1); c2.set_time(6); c2.show_time(); return 0; }
实验任务4
#include<iostream> class X{ public: X(); ~X(); X(int m); X(const X & obj); X(X && obj) noexcept; void show() const; private: int data; }; X::X(): data{42}{ std::cout << "default constructor called.\n"; } X::~X(){ std::cout << "destructor called.\n"; } X::X(int m): data{m}{ std::cout << "constructor called.\n"; } X::X(const X & obj) : data{obj.data}{ std::cout << "copy constructor called.\n"; } X::X(X && obj) noexcept: data{obj.data}{ std::cout <<"move constructor called.\n"; } void X::show() const { std::cout << data << std::endl; } int main(){ X x1; x1.show(); X x2{2049}; x2.show(); X x3{x1}; x3.show(); X x4{std::move(x2)}; x4.show(); return 0; }
在43行时,x1,调用默认构造函数 X()
在46行时,x2,调用含参数的构造函数 X(int m)
在49行时,x3,调用复制构造函数 X(const X & obj)
在52行时,x4,调用移动构造函数 X(X && obj)
主函数结束前,x1,x2,x3,x4, 都调用了析构函数 ~X()
实验任务5
#include<iostream>
#include<iomanip>
class Rectangle{
public:
Rectangle();
Rectangle(double c, double k);
Rectangle(const Rectangle & obj);
void resize(double l_times, double w_times);
void resize(double times);
double len() const ;
double wide() const ;
double area() const ;
double circumference() const ;
private:
double length, width;
};
Rectangle::Rectangle(){
length = 2;
width = 1;
}
Rectangle::Rectangle(double len, double wide){
length = len;
width = wide;
}
Rectangle::Rectangle(const Rectangle & obj) : length{obj.length}, width{obj.width}{}
void Rectangle::resize(double l_times, double w_times){
length *= l_times;
width *= w_times;
}
void Rectangle::resize(double times){
length *= times;
width *= times;
}
double Rectangle::circumference() const{
return (length + width) * 2;
}
double Rectangle::area() const {
return length * width;
}
double Rectangle::len()const {
return length;
}
double Rectangle::wide()const {
return width;
}
void output(const Rectangle & rect){
using namespace std;
cout << "矩阵信息:\n";
cout << fixed << setprecision(2);
cout << "长: " << rect.len() << endl;
cout << "宽: " << rect.wide() << endl;
cout << "面积: " << rect.area() << endl;
cout << "周长: " << rect.circumference() << endl;
cout << endl;
}
int main(){
Rectangle rect1;
output(rect1);
Rectangle rect2(10, 5);
output(rect2);
Rectangle rect3(rect1);
rect3.resize(2);
output(rect3);
rect3.resize(5, 2);
output(rect3);
}
实验总结:
一些收获:
1.一个const对象不能调用它的non-const成员函数,只能使用const成员。半天过不了编译,百度的结果
2.虽然"private:"的函数外部不能访问,但是我们可以写"public:"来访问它
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本