C++编程:类与对象

要求:
(1) 了解静态对象的定义和使用方法;
(2) 掌握静态数据成员和静态成员函数的定义和使用方法;
(3) 理解类的作用域、对象的作用域及生存周期;
(4) 掌握函数调用中参数的传递;
(5) 掌握常量类型;
(6) 掌握友元函数和友元类的定义及使用。

1.定义一个矩形类Rectangle,矩形的左上角(Left,Top)与右下角坐标(Right,Bottom)定义为保护数据成员。用公有成员函数Diagonal()计算出矩形对角线的长度,公有成员函数Show()显示矩形左上角与右下角坐标及对角线长度。在主函数中用new运算符动态建立矩形对象r1,初值为(10,10,20,20)。然后调用Show()显示矩形左上角与右下角坐标及对角线长度。最后用delete运算符回收为矩形动态分配的存储空间。

#include<bits\stdc++.h>
using namespace std;
class Rectangle
{
    protected:
    double diagonal;
    public:

    void Diagonal(int left,int top,int right,int bottom)
    {
        Right=right;
        Left=left;
        Top=top;
        Bottom=bottom;
        diagonal=sqrt((Left-Right)*(Left-Right)+(Top-Bottom)*(Top-Bottom));
    }
    void Show()
    {
        cout<<"左上角坐标:"<<Left<<","<<Top<<endl;
        cout<<"右上角坐标:"<<Right<<","<<Bottom<<endl;
        cout<<"对角线长度:"<<diagonal<<endl;
    }
    protected:
        int Left,Top;
        int Right,Bottom;
};
int main()
{
    Rectangle *r1;
    r1=new Rectangle;
    r1->Diagonal(10,10,20,20);
    r1->Show();

    //delete r1;
    system("pause");
    return 0;
}

在这里插入图片描述

2.输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、最高成绩、最低成绩。

#include<bits\stdc++.h>
//输出每个学生的姓名、学号、成绩;(2)统计并输出学生的总人数、总成绩、平均成绩、
//最高成绩、最低成绩。
using namespace std;
class Student
{
    
    string name;
    long long ID;//学号
    static int num;//学生总人数
    static float avera;//平均成绩
    float per;//成绩
    //float Eng;//英语成绩
    static float scor;//总成绩
    static float hig;//最高成绩
    static float low;//最低成绩
    public:
    Student(string a,long long b,float s);//定义构造函数
    void chu();
    void Show();//输出每个学生的姓名、学号、成绩
    static void Num();
    static void s();//输出最高成绩,最低成绩
    static void score();//输出总成绩
};
int Student::num=0;
float Student::hig=0;
float Student::low=0;
float Student::avera=0;
float Student::scor=0;
Student::Student(string a,long long b,float s)
{
    name=a;
    ID=b;
    per=s;
    //Eng=c;
    scor+=s;
    hig=hig>per?hig:per;
    low=low>per?per:low;
    
}
void Student::chu()
{
    scor=per;
    low=per;
}

void Student::Show()
{
    num++;

    cout<<name<<" "<<ID<<" "<<"成绩:"<<per<<" "<<endl;
}
void Student::Num()
{
    cout<<"学生总人数是:"<<num<<endl;
}
void Student::s()
{
    avera=scor/num;
    cout<<"总成绩是:"<<scor<<endl;
    cout<<"平均成绩是:"<<avera<<endl;
    cout<<"最高成绩是:"<<hig<<endl;
    cout<<"最低成绩是:"<<low<<endl;
}
int main()
{
    Student s1("陈琳林",2020002360,90);
    s1.chu();
    s1.Show();
    Student s2("魏源昕",2020002361,91);
    s2.Show();
    Student s3("张云潇",2020002363,92);
    s3.Show();
    Student::Num();
    Student::s();
    system("pause");
    return 0;
}

3.设计一个程序,其中3个类CBabk、BBabk、GBank分别为中国银行类、工商银行类和农业银行类。每个类都包含一个私有数据balance,用于存放储户在该行的存款数,另一个友元函数total用于计算储户在这3家银行中的总存款数。

#include<bits\stdc++.h>
using namespace std;
//中国银行类
class BBank;
class GBank;
class CBank
{
    public:

    friend int total(CBank a,BBank b,GBank c);
    private:
        int balance=100;
};
//工商银行类
class BBank
{
    public:
    friend int total(CBank a,BBank b,GBank c);
    private:
        int balance=230;
};
//农业银行类
class GBank
{
    public:
    friend int total(CBank a,BBank b,GBank c);
    private:
        int balance=200;
};
int total(CBank a,BBank b,GBank c)
{
    return a.balance+b.balance+c.balance;
}
int main()
{
    CBank A;
    BBank B;
    GBank C;
    cout<<"三个银行的总存款数是:"<<endl;
    cout<<total(A,B,C)<<endl;
    system("pause");
    return 0;
}

在这里插入图片描述

注意

  1. 注意用new申请空间要用delete来释放;
  2. 友元函数要正确使用,合理的使用其能访问私有数据成员的功能;
  3. 注意传常引用不能改变数据原本的值,搭配友元函数实现数据的保护;
  4. sqrt函数的使用,开根号;
  5. 用vector定义动态数组,vector 是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器。vector 是C++ STL的一个重要成员
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在类的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积和周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@小冯@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值