c++的三个实验

文章涉及C++编程实验,包括类的构造函数和析构函数的使用,成员数据的初始化,以及继承和访问权限的理解。实验中展示了如何定义和初始化对象,以及处理编程过程中的错误,如成员函数和数据成员的混淆,以及this指针的运用。还讨论了不同继承方式下基类成员在派生类中的访问权限变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意格式

1.在那个框里写好内容后,还要再通过添加附件加上一个word文档(一定记得改掉我的学号和名字)
具体如图
c++的三个实验-0
c++的三个实验-1
c++的三个实验-2

实验1

文本框里

1.代码#include
#include
using namespace std;
class Fun {
       private:
              int speed;
              int radius;
              string color;
              string zt;//风扇状态
       public:
              Fun(int s,int r,string c,string o) {
                     speed=s;
                     radius=r;
                     color=c;
                     zt=o;
              }
              display() {
                     cout<<“speed:”<<speed<<endl;
                     cout<<“radius:”<<radius<<endl;
                     cout<<“color:”<<color<<endl;
                     cout<<“zt:”<<zt<<endl;
              }//初始化
       };
              int main(){
                     Fun fun1(3,10,“yellow”,“on”);
                     Fun fun2(2,5,“blue”,“off”);
                     fun1.display();
                     fun2.display();
              return 0;
              }
2.遇到的问题及解决方法
错误1:忘记在类后的括号后加“;”
错误2:编译时没问题,但是输出结果不对。后来把s=speed,改成speed=s,运
行结果正确。因为你是要把实参赋给形参,s是形参。
3.问题:
1.定义类时,那些成员数据和成员函数是必须的,那些不是?
答:speed,ridius,color,zt是必须的,display,fun也是必须的。
但fun函数也可以换成其他可以对speed,ridius,color,zt初始化的函数。类似get函数那种。
2.public、protected、private分别表示什么意思?
答:Public类型代表公开类型,意味着你可以从类外访问这种类型;
protect类型代表保护类型,意味着你不可以从类外访问这种类型,只能从类内访问,继承时子类可继承;
private类型代表私有类型,意味着你不可以从类外访问这种类型,只能从类内访问,任何继承时子类都不可继承;
3.能否对类的成员数据在定义时初始化?
答:在C++11标准及之后可以,但之前的不行,只能通过构造函数,和参数初始化列表等方式初始化。因为定义的时候只是一个声明,只有在初始化的时候才实际分配内存空间。次序不能乱。
4.运行截图
c++的三个实验-3

word文档

[2031052113_张翰冰_C 实验1.docx](assets/2031052113_张翰冰_C 实验1.docx)

实验2

文本框里

1.代码
#include
using namespace std;
class CPoint
{public:
CPoint(int x=0,int y=0){this->x=x;this->y=y;}
//this指针指向当前对象,即将x的值赋给当前对象的的x属性
int GetX(){ return x;}
int GetY(){ return y;}
void SetX(int x){ this->x=x;}
void SetY(int y){ this->y=y;}
private:
int x,y;
};
class CRectangle
{public:
CRectangle(){}
CRectangle(const CPoint &LPoint,const CPoint &RPoint){ this->LPoint=LPoint;this->RPoint=RPoint;}
void SetLPoint(CPoint lp){ LPoint.SetX(lp.GetX()); LPoint.SetY(lp.GetY()); }
void SetRPoint(CPoint rp){ RPoint.SetX(rp.GetX()); RPoint.SetY(rp.GetY()); }
int GetPerimeter(){return 2*(RPoint.GetX()-LPoint.GetX()+RPoint.GetY()-LPoint.GetY());}
int GetArea(){ return (RPoint.GetX()-LPoint.GetX())*(RPoint.GetY()-LPoint.GetY());}
private:
CPoint LPoint,RPoint;
};
int main(){
CPoint p1(2,5),p2(6,8);
CRectangle *p=new CRectangle(p1,p2);//动态创建一个CRectangle类的对象
cout<<“c1=”<GetPerimeter()<<endl;//获得矩形周长
cout<<“s1=”<GetArea()<<endl;//获得矩形面积
CRectangle a_rectagnle;
CPoint p3(4,6),p4(7,9);
a_rectagnle.SetLPoint(p3);//设置a_rectagnle的左下角为(4,6)
a_rectagnle.SetRPoint(p4);//设置a_rectagnle的右上角为(7,9)
cout<<“c2=”<<a_rectagnle.GetPerimeter()<<endl;//获得矩形周长
cout<<“s2=”<<a_rectagnle.GetArea()<<endl;//获得矩形面积
delete p;
return 0;
}
2. 实验报告中记录编程过程中出现的错误与改正方法;
答:开始时使用的是参数初始化列表进行初始化,后来发现容易搞混形式参数和类内的数据成员,后来换成this指针,感觉很好用,this指针永远指向当前对象的特性让写构造函数省心不少。
另外这次代码比上次难一些,第一次靠自己写的代码问题不少,最后慢慢对比网上的代码改好,对this指针和动态创建对象和初始化这块的知识还有点陌生。
c++的三个实验-4

1、 总结类的构造函数和析构函数的特点;
答:类的构造函数特点:
-构造函数是成员函数可以写在类外,但是必须类内声明。
-一个类内只能定义一个构造函数,但可以通过重载,定义不同参数的构造函数。
-类内如果不写构造函数,会自动调用默认构造函数。
类的析构函数特点:
-析构函数是特殊成员函数,与类同名,但前面多一个“~”。
-一个类内只能有一个析构函数,并且不能重载,因为析构函数不指定数据类型,并且也没有参数。
-如果一个对象是new出来的,那么在delete的时候会自动调用析构函数析构

word文档

实验2_2031052113__张翰冰.docx

实验3

文本框内

1.代码

#include
#include <math.h>
#include
using namespace std;

/* Person /
class Person
{
public:
/

Person(string name1,string id1,string phonenumber1)
{name=name1;id=id1;phonenumber=phonenumber1;}
*/
void person(string a,string b,string c)
{
name=a;
id=b;
phonenumber=c;
}
void print_person()
{
cout << "Name: "<<name << " Id: " << id << " Phonenumber: " << phonenumber << endl;
}
~Person(){}
private:
string name,id,phonenumber;
};

/* MyDate */
class MyDate
{
public:
double diffYear(MyDate &a)
{
int sum;
sum = 12 * (year-a.year-1) + fabs(12-a.month); //??è·μ???
return double(sum);
}
void Input_year(int y)
{
year=y;
}
void Input_month(int m)
{
month=m;
}
void Input_day(int d)
{
day=d;
}
int Output_year()
{
return year;
}
int Output_month()
{
return month;
}
int Output_day()
{
return day;
}
void print_MyDate()
{
cout << "MyDate: " << year << “/” << month << “/” << day << endl;
}
private:
int year,month,day;
};

/* Student */
class Student:public Person
{
public:
Student(string a):grade(a){}
void print_student()
{
cout << "grade: " << grade << endl;
}
private:
const string grade;
};

/* Employee /
class Employee:public Person
{
public:
/

Employee(string name1,string id1,string phonenumber1,string office1,double salary1,MyDate dateHired1):Person(name1,id1,phonenumber1)
{
office=office1;
salary=salary1;
dateHired=dateHired1;
}
*/
void Input_office(string a)
{
office=a;
}
void Input_salary(double b)
{
salary=b;
}
void Input_dateHired(MyDate &d)
{
dateHired = d;
}
void print_employee()
{
cout << "office: " << office << " ";
cout << "雇佣日期: " << dateHired.Output_year() << “/” << dateHired.Output_month() << “/” << dateHired.Output_day() << endl;
}
private:
string office;
double salary;
MyDate dateHired;
};

/* Faculty */
class Faculty:public Employee
{
public:
Faculty(int a)
{
rank = a;
}
private:
int rank;
};

/* Staff */
class Staff:public Employee
{
public:
void Input_Position(string a)
{
position = a;
}
void Input_BasicWages(int b)
{
BasicWages=b;
}
void Input_Allowance(int c)
{
Allowance = c;
}
int Output_BasicWages()
{
return BasicWages;
}
int Output_Allowance()
{
return Allowance;
}
void print_staff()
{
cout << "Position: " << position << endl;
}
private:
string position;
int BasicWages,Allowance;
};

int main()
{
int rank1,year1,month1,day1;
int faculty_wages,staff_wages;
MyDate date,m1,m2;
Person person1;
//Person person1(“HangZhou”,“110”,“10086”);
Student student1(“Freshman”);
Employee employee1;
Faculty faculty1(2);
Staff staff1;

person1.person(“HangZhou”,“110”,“10086”);
cout << "输入雇佣日期,year/month/day: " << endl;
cin >> year1 >> month1 >> day1 ;
date.Input_year(year1);
date.Input_month(month1);
date.Input_day(day1);
employee1.Input_office(“行政楼”);
employee1.Input_salary(3000.0);
employee1.Input_dateHired(date);
staff1.Input_BasicWages(5000);
staff1.Input_Allowance(1000);
cout << "输入教师级别,3/2/1: " << endl;
cin >> rank1;
cout << "输入教工开始工作时间(小于2010),year/month : " << endl;
cin >> year1 >> month1 ;
m1.Input_year(year1);
m1.Input_month(month1);
m2.Input_year(2010);
m2.Input_month(1);
faculty_wages = (staff1.Output_BasicWages()) ;
staff_wages = staff1.Output_BasicWages() + (staff1.Output_Allowance() * (m2.diffYear(m1)/12));

person1.print_person(); //Person
cout << "Student: " ; //Student
student1.print_student();
cout << "Employee: " ; //Employee
employee1.print_employee();
cout << "教师工资: " << faculty_wages << endl; //Faculty
cout << "教工工资: " << staff_wages << endl; //Staff
return 0;
}
2.截图
c++的三个实验-5

1、总结基类中public、protected、private成员在不同的继承方式下在派生类中的访问权限;
公有继承(public)
基类的public和protected成员的访问属性在派生类中保持不变,但基类的private成员不可直接访问;
派生类中的成员函数可以直接访问基类中的public和protected成员,但是不能直接访问基类的private成员;
通过派生类的对象只能访问基类的public成员。
私有继承(private)
基类的public和protected成员都以private身份出现在派生类中,但基类的private成员不可以直接访问;
派生类中的成员函数可以直接访问基类中的public和protected成员,但是不能直接访问基类的private成员;
通过派生类的对象不能访问基类的任何成员。
保护继承(protected)
基类的public和protected成员都以protected身份出现在派生类中,但基类的private成员不可以直接访问;
派生类中的成员函数可以直接访问基类中的public和protected成员,但是不能直接访问基类的private成员;
通过派生类的对象不能访问基类的任何成员。

word文档

实验3_2031052113__张翰冰.docx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值