分课堂
20250807C++课堂
阶段性测试
探讨除法分配律
探讨平行四边形、三角形、梯形的面积公式
Scratch版微信飞机大战
走迷宫算法
关于0.1+0.2=0.30000000000000004
信息系统项目管理师
Scratch《无人机艺术树》编程教案
Scratch《近防炮模拟系统》完整教案
Scratch《左手摸墙法自动走迷宫》编程教案
Scratch 贪吃蛇游戏开发教案
本文档使用 MrDoc 发布
-
+
首页
20250807C++课堂
## 一、C++入门 http://docs.chuangke.tech/doc/88/ ## 二、C++初级 课程名称:[C++] 简单图形的打印 课程目标: 1.打印矩形 2.打印直角三角形 3.打印倒直角三角形 4.打印侧等腰三角形 5.打印空心矩形 http://docs.chuangke.tech/doc/129/ ## 三、C++高级 ``` //**************************** //第五章 特殊函数和成员 //**************************** ``` ### 5.1分析下面程序中析构函数与构造函数的调用顺序*/ ``` #include <iostream> using namespace std; class object { private: int val; public: object():val(0) { cout<<"Defalt constructor for object"<<endl; } object(int i):val(i) { cout<<"Consructor for object"<<val<<endl; } ~object() { cout<<"Destructor for object"<<val<<endl; }; class container { private: object one; object two; int data; public: container():data(0) { cout<<"Defalt constructor for object"<<endl; } container(int i,int j,int k); ~container() { cout<<"Consructor for object"<<data<<endl; }; container()::container(int i,int j,int k):two(i),one(j) { data=k; cout<<"Constructor for container"<<data<<endl; } void main() { constainer obj, anObj(5,6,10); } ``` ### /*5.2分析下面程序的输出结果*/ ``` class Test { static int x; int n; public: Test() { } Test(int a,int b) { x=a; n=b; } static int func() { return x; } static void sfunc(Test&r,int a) { r.n=a; } int Getn() { return n; } }; int Test::x=25; #include <iostream> using namespace std; void main() { cout<<Test::func(); Test b,c; b.sfunc(b,58); cout<<" "<<b.Getn(); cout<<" "<<b.func(); cout<<" "<<c.func(); Test a(24,56); cout<<" "<<a.func()<<" "<<b.func()<<" "<<c.func()<<endl; } ``` ### /*5.3使用表态对象的例子*/ ``` #include <iostream> using namespace std; class test { private: int n; public: test(int i) { n=i; cout<<"constructor:"<<i<<endl; } ~test() { cout<<"destructor:"<<n<<endl; } int getn() { return n; } void inc() { ++n; } }; void main() { cout<<"loop start:"<<endl; for(int i=0;i<3;i++) { static test a(3); test b(3); a.inc(); b.inc(); cout<<"a.n="<<a.getn()<<endl; cout<<"b.n="<<b.getn()<<endl; } cout<<"loop end."<<endl; cout<<"Exit main()"<<endl; } ``` ### /*5.4使用友元函数计算两点之间距离的例子*/ ``` #include <iostream> #include <cmath> using namespace std; class Point { private: double X,Y; public: Point(double xi,double yi) { X=xi; Y=yi; } double GetX() { return X; } double GetY() { return Y; } friend double distances(Point&,Point&); }; double distances(Point&a,Point&b) { double dx=a.X-b.X; double dy=a.Y-b.Y; return sqrt(dx*dx+dy*dy); } void main() { Point p1(3.5,5.5),p2(4.5,6.5); cout<<"The distance is"<<distances(p1,p2)<<endl; } ``` ### /*5.5类One的对象通过友元函数访问类TWO的对象的私有数据*/ ``` #include <iostream> using namespace std; class Two; class One { private: int x; public: One(int a) { x=a; } int Getx() { return x; } void func( Two&); }; class Two { private: int y; public: Two(int b) { y=b; } int Gety() { return y; } friend void One::func( Two&); }; void One::func( Two& r) { r.y=x; } void main() { One Obj1(5); Two Obj2(8); cout<<Obj1.Getx()<<" "<<Obj2.Gety()<<endl; Obj1.func(Obj2); cout<<Obj1.Getx()<<" "<<Obj2.Gety()<<endl; } ``` ### /*5.6*/ ```cpp #include <iostream> using namespace std; class Two { int y; public: friend class One; }; class One { int x; public: One(int a,Two&r,int b) { x=a; r.y=b; } void Display( Two&); }; void One::Display( Two& r) { cout<<x<<" "<<r.y<<endl; } void main() { Two Obj2; One Obj1(23,Obj2,55); Obj1.Display(Obj2); } ``` ### /*5.7常数据成员初始化和常引用作为函数参数*/ ```cpp #include <iostream> using namespace std; class Base { private: int x; const int a; static const int b; const int& r; public: Base(int ,int); void Show() { cout<<x<<","<<a<<","<<b<<","<<r<<endl; } void Display(const double &r) { cout<<r<<endl; } }; const int Base::b=125; Base::Base(int i,int j):x(i),a(j),r(x) { } void main() { Base a1(104,118),a2(119,140); a1.Show(); a2.Show(); a2.Display(3.14159); } ``` ### /*5.8const对象调用const成员函数*/ ```cpp #include <iostream> using namespace std; class Base { private: double x,y; const double p; public: Base(double m,double n,double d):p(d) { x=m; y=n; } void Show(); void Show()const; }; void Base::Show() { cout<<x<<","<<y<<" p="<<p<<endl; } void Base::Show()const { cout<<x<<","<<y<<" const p="<<p<<endl; } void main() { Base a(8.9,2.5,3.1416); const Base b(2.5,8.9,3.14); b.Show(); a.Show(); } ``` ### /*5.9const函数返回的常数对象与其他常量对象一起使用*/ ```cpp #include <iostream> using namespace std; class ConstFun { public: int f5()const { return 5; } int Obj() { return 45; } }; void main() { ConstFun s; const int i=s.f5(); const int j=s.Obj(); int x=s.Obj(); int y=s.f5(); cout<<i<<" "<<j<<" "<<x<<" "<<y; const ConstFun d; int k=d.f5(); cout<<"k="<<k<<endl; } ``` ### /*5.10使用类对象数组和指针的例子*/ ``` class Test { int num; double f1; public: Test(int n) { num=n; } Test(int n,double f) { num=n; f1=f; } int GetNum() { return num; } double GetF() { return f1; } }; #include <iostream> using namespace std; void main() { Test one[2]={2,4},*p; Test two[2]={Test(1,3.2),Test(5,9.5)}; for (int i=0;i<2;i++) cout<<"two["<<i<<"]="<<one[i].GetNum()<<endl; p=two; for(i=0;i<2;i++,p++) cout<<"two["<<i<<"]=("<<p->GetNum()<<","<<p->GetF()<<")"<<endl; } ``` ### /*5.11仍然使用类Test,但改用对象指针数组的演示主程序*/ ``` #include <iostream> using namespace std; void main() { Test *one[2]={new Test(2),new Test(4)}; Test *two[2]={new Test(1,3.2),new Test(5,9.5)}; for (int i=0;i<2;i++) cout<<"one["<<i<<"]="<<one[i]->GetNum()<<endl; for(i=0;i<2;i++) cout<<"two["<<i<<"]=("<<p->GetNum()<<","<<p->GetF()<<")"<<endl; } ``` ### /*5.12使用指向类成员函数的指针*/ ``` #include <iostream> using namespace std; class A { private: int val; public: A(int i) { val=i; } int value(int a) { return val+a; } }; void main() { int(A::*pfun)(int); pfun=A::value; A obj(10); cout<<(obj.*pfun)(15)<<endl; A *pc=&obj; cout<<(pc->*pfun)(15)<<endl; } /*解一元二次方程*/ /*头文件equation.h*/ #if ! defined(EQUATION_H) #define EQUATION_H #include <iostream> #include <cmath> using namespace std; class FindRoot { private: float a,b,c,d; double x1,x2; public: FindRoot(float x,float y,float z); void Find(); void Display(); }; #endif /*equation.cpp实现类*/ #include "equation.h" FindRoot::FindRoot(float x,float y,float z) { a=x; b=y; c=z; d=b*b-4*a*c; } void FindRoot::Find() { if(d>0) { x1=(-b+sqrt(d))/(2*a); x2=(-b-sqrt(d))/(2*a); return; } else if (d==0) { x1=x2=(-b)/(2*a); return; } else { x1=(-b)/(2*a); x2=sqrt(-d)/(2*a); } } void FindRoot::Display() { if(d>0) { cout<<"x1="<<x1<<"\nx2="<<x2<<endl; return; } else if (d==0) { cout<<"x1=x2="<<x1<<endl; return; } else{ cout<<"x1="<<x1<<"+"<<x2<<"i"<<endl; cout<<"x2="<<x1<<"-"<<x2<<"i"<<endl; } } /*主函数Find.cpp*/ #include "equation.h" void Read(float&,float&,float&); void main() { float a,b,c; cout<<"这是个一元二次方程AX^2+BX+C=0的根的方程。"<<endl; for(;;) { Read(a,b,c); if (a==0) return; FindRoot obj(a,b,c); obj.Find(); obj.Display(); } } void Read(float& a,float& b,float& c) { cout<<"输入方程系数a:"; cin>>a; if (a==0) { getchar(); return; } cout<<"输入方程系数b:"; cin>>b; cout<<"输入方程系数c:"; cin>>c; } ``` ## 第四章课后 一、填空题 1.数据成员、成员函数; 2.类、析构函数不允许有参数和返回类型(可是显示地说明参数为void)、一个类有1个析构函数; 3.`fun::fun(fun &)、fun::fun(const fun &);` 二、单项选择题 1.C。2.C。3.没又答案,应该是`A::~A(void)`、或A::~A()。4.B。 5.C。 6.C。 7.D 三、改错题 1.return m;---错误,没又定义变量m; 2.A.init(24,56);---错误,没有声明对象A,init函数参数不对应; Setx函数是int型但是没有返回值 四、完成程序题 1. ``` #include < iostream > using namespace std; class base { private : //私有数据成员 int a, b; public : void init(int x, int y)//公有函数 { a = x; b = y; } void print() { cout<<"2 * "<< a<<" - "<< b<<" = "<<(2*a-b)<< endl; } }; void main() { base a; a.init(68,55); a.print(); } ``` 2. ``` #include using namespace std; class Point { private : int m, n; public : Point(int, int);//整型变量,为参数的构造函数 Point(Point&);//复制构造函数的原型 print() { cout<<"m = "<< m<<", n = "<< n<< endl; } }; Point::Point(int a, int b) { m = a; n = b; } Point::Point(Point & t)//复制构造函数的定义 { m = t.m; n = t.n; } void main() { Point a(10,89); Point b(a); a.print(); b.print(); } ``` 五、程序分析题 1.没有结果,因为没有main函数 如果加main函数 void main() { base b(10, 20); } 输出: 初始化...10,20 Destory...10,20 2. 输出: 55 六、编程题 1.设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形的对角顶点。并可以输出4个坐标值和面积。使用测试程序验证程序。 ``` #include < iostream.h > using namespace std; class Point //点类 { private: int x, y;//私有成员变量,坐标 public : Point()//无参数的构造方法,对xy初始化 { x = 0; y = 0; } Point(int a, int b)//又参数的构造方法,对xy赋值 { x = a; y = b; } void setXY(int a, int b)//设置坐标的函数 { x = a; y = b; } int getX()//得到x的方法 { return x; } int getY()//得到有的函数 { return y; } }; class Rectangle //矩形类 { private: Point point1, point2, point3, point4;//私有成员变量,4个点的对象 public : Rectangle();//类Point的无参构造函数已经对每个对象做初始化啦,这里不用对每个点多初始化了 Rectangle(Point one, Point two)//用点对象做初始化的,构造函数,1和4为对角顶点 { point1 = one; point4 = two; init(); } Rectangle(int x1, int y1, int x2, int y2)//用两对坐标做初始化,构造函数,1和4为对角顶点 { point1.setXY(x1, y1); point4.setXY(x2, y2); init(); } void init()//给另外两个点做初始化的函数 { point2.setXY(point4.getX(), point1.getY() ); point3.setXY(point1.getX(), point4.getY() ); } void printPoint()//打印四个点的函数 { cout<<"A:("<< point1.getX() <<","<< point1.getY() <<")"<< endl; cout<<"B:("<< point2.getX() <<","<< point2.getY() <<")"<< endl; cout<<"C:("<< point3.getX() <<","<< point3.getY() <<")"<< endl; cout<<"D:("<< point4.getX() <<","<< point4.getY() <<")"<< endl; } int getArea()//计算面积的函数 { int height, width, area; height = point1.getY() - point3.getY(); width = point1.getX() - point2.getX(); area = height * width; if(area > 0) return area; else return -area; } }; void main() { Point p1(-15, 56), p2(89, -10);//定义两个点 Rectangle r1(p1, p2);//用两个点做参数,声明一个矩形对象r1 Rectangle r2(1, 5, 5, 1);//用两队左边,声明一个矩形对象r2 cout<<"矩形r1的4个定点坐标:"<< endl; r1.printPoint(); cout<<"矩形r1的面积:"<< r1.getArea() << endl; cout<<"\n矩形r2的4个定点坐标:"<< endl; r2.printPoint(); cout<<"矩形r2的面积:"<< r2.getArea() << endl; } ``` 2.使用内联函数设计一个类,用来表示直角坐标系中的任意一条直线并输出它的属性。 ``` #include < iostream.h > #include < math.h > class Line { private: int x1, y1, x2, y2; public : Line(); Line(int =0, int =0, int =0, int=0 ); void printPoint(); double getLength(); }; inline Line::Line(int a, int b, int c, int d) { x1 = a; y1 = b; x2 = c; y2 = d; } inline void Line::printPoint() { cout<<"A:"<< x1 <<", "<< y1 << endl; cout<<"B:"<< x2 <<", "<< y2 << endl; } inline double Line::getLength() { double length; length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) ); return length; } void main() { Line line(10,80,-10,12); line.printPoint(); cout<< line.getLength() << endl; } ``` ```
admin
2025年8月11日 09:52
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码