c++


#include<bits/stdc++.h>
using namespace std;
class Goods
{
public:
    Goods(int w,string s);
    void sell();
    int Getweight() const;
    static int GetTo();
    const string name;
private:
    static int toalweight;
    int weight;

};
Goods::Goods(int w,string s):name(s)
{
    weight=w;
    toalweight+=weight;
    cout<<name<<endl;
}
void Goods::sell()
{
    toalweight-=weight;
    //weight=0;
}
int Goods::Getweight() const
{
    return weight;
}
int Goods::GetTo()
{
    return toalweight;
}
int Goods::toalweight=0;
int main()
{
    Goods a(10,"fsdaf");
    Goods b(20,"fdsafsa");
    Goods c(30,"chen");
    cout<<Goods::GetTo()<<endl;
    a.sell();
    b.sell();
    cout<<Goods::GetTo()<<endl;
    cout<<a.name<<endl;

    return 0;
}

6.13

#include<iostream>
using namespace std;
class con
{
protected:
    double r;
public:
    //con(){}
    con(double r1)
    {
        r=r1;
        cout<<"dfsa"<<r<<endl;
    }
    virtual double s_a()=0;
    virtual double v()=0;
};
class cu:public con
{
private:
    int a;
public:
    cu(double a1):con(a1)
    {
        a=a1;
    }

    virtual double s_a()
    {
        cout<<"正方形表: "<<6*a<<endl;
    }
    virtual double v()
    {
        cout<<"正方形体: "<<a*a*a<<endl;
    }
};
class sp:public con
{

public:
    sp(double r1):con(r1){}
    virtual double s_a()
    {
        cout<<"球表:"<<4*3.17*r*r<<endl;
    }
    virtual double v()
    {
        cout<<"球体: "<<1.0*4/3*3.14*r*r*r<<endl;
    }
};
class cy:public con
{
private:
    double r,h;
public:
    cy(double r1,double h1):con(r1)
    {
        r=r1;
        h=h1;
    }
    virtual double s_a()
    {
        cout<<"圆柱表: "<<3.14*r*r<<endl;
    }
    virtual double v()
    {
        cout<<"圆柱体: "<<3.14*r*r*h<<endl;
    }
};
int main()
{
    con *p;
    cu c(3.0);
    cu w(2.0);
    p=&w;
    p->s_a();
    p->v();
    sp a(6.0);
    p=&a;
    p->s_a();
    p->v();
    cy ss(1.0,2.0);
    p=&ss;
    p->s_a();
    p->v();
    return 0;
}

类的组合

#include<bits/stdc++.h>
using namespace std;
class x
{
public:
    x(int a1,double b1)
    {
        a=a1;
        b=b1;
    }
    void disp()
    {
        cout<<"a="<<a<<endl;
        cout<<"b="<<b<<endl;
    }
private:
    int a;
    double b;
};
class y
{
public:
    y(int a1,double b1,int c1):xx(a1,b1)
    {
        c=c1;
    }
    void disp()
    {
        xx.disp();
        cout<<"c="<<c<<endl;
    }
private:
    x xx;
    int c;
};
int main()
{
    y a(123,2.3,985);
    a.disp();
    return 0;
}

7.11

#include<bits/stdc++.h>
using namespace std;
class A
{
public:
    A()
    {

        for(int i=0;i<2;i++)
        {
            for(int j=0;j<3;j++)
            {
                s[i][j]=0;
            }
        }
    }
    A(int a,int b,int c,int d,int e,int f)
    {
        s[0][0]=a;
        s[0][1]=b;
        s[0][2]=c;
        s[1][0]=d;
        s[1][1]=e;
        s[1][2]=f;
    }
    void w()
    {
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<3;j++)
            {
                cin>>s[i][j];
            }
        }
    }
    friend A operator + (A x,A y)
    {
         A t;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<3;j++)
            {
                t.s[i][j]=x.s[i][j]+y.s[i][j];
            }
        }
        return t;
    }
    void show()
    {
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<3;j++)
            {
                cout<<"i="<<i<<"  j="<<j<<"  "<<s[i][j]<<"  ";
            }
            cout<<endl;
        }
    }
private:
    int s[2][3];
};
int main()
{
    A a(1,2,3,4,5,6),b,c;
    cout<<"a"<<endl;
    a.show();
    b.w();
    cout<<"b"<<endl;
    b.show();

    //c=a+b;
    c=operator +(a,b);
    cout<<"c"<<endl;
    c.show();
    return 0;
}

7.12

#include <iostream>
using namespace std;
class Matrix
 {public:
   Matrix();
   friend Matrix operator+(Matrix &,Matrix &);
   friend ostream& operator<<(ostream&,Matrix&);
   friend istream& operator>>(istream&,Matrix&);
  private:
   int mat[2][3];
 };

Matrix::Matrix()
{
  for(int i=0;i<2;i++)
  for(int j=0;j<3;j++)
   mat[i][j]=0;
}
Matrix operator+(Matrix &a,Matrix &b)
{
    Matrix c;
   for(int i=0;i<2;i++)
   for(int j=0;j<3;j++)
     {
         c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
     }
   return c;
}
istream& operator>>(istream &in,Matrix &m)
{
    cout<<"input value of matrix:"<<endl;
   for(int i=0;i<2;i++)
    for(int j=0;j<3;j++)
    in>>m.mat[i][j];
  return in;
}

ostream& operator<<(ostream &out,Matrix &m)
{
    for (int i=0;i<2;i++)
  {
   for(int j=0;j<3;j++)
   {out<<m.mat[i][j]<<" ";}
    out<<endl;}
   return out;
}
int main()
{ Matrix a,b,c;
 cin>>a;
 cin>>b;
 cout<<endl<<"Matrix a:"<<endl<<a<<endl;
 cout<<endl<<"Matrix b:"<<endl<<b<<endl;
 c=a+b;
 cout<<endl<<"Matrix c = Matrix a + Matrixb :"<<endl<<c<<endl;
 return 0;
}

6.14

#include<iostream>  
using namespace std;  
const double PI= 3.14;  
  
class container{  
public:  
    container(double r1)  
    {  
        r = r1;  
    };  
    container(double r1,double h1 )  
    {  
        r = r1;  
        h= h1;  
  
    };  
    virtual void surface_area() = 0;  
    virtual void volume() = 0;  
protected:  
        double r;  
        double h;  
};  
class cube:public container  
{  
public:  
    cube(double a) :container(a)  
    {}  
    void surface_area()  
    {  
        cout << "该正方体的边长为:" << r << "表面积为:" << 6*r*r<<endl;  
    }  
    void volume()  
    {  
        cout << "该正方体的边长为:" << r << "体积为:" << r*r*r <<endl;  
    }  
};  
class sphere :public container  
{  
public:  
    sphere(double a) :container(a)  
    {}  
    void surface_area()  
    {  
        cout << "该球体的半径为:" << r << "表面积为:" << 4*PI*r*r << endl;  
    }  
    void volume()  
    {  
        cout <<  "该球体的半径为:"  << r<< "体积为:" <<4/3*PI*r*r*r << endl;  
    }  
};  
class cylinder :public container  
{  
public:  
    cylinder(double a,double b) :container(a,b)  
    {};  
    void surface_area()  
    {  
        cout << "该圆柱体的半径为:" << r << "高为:" << h << "表面积为:" << PI*r*r+2*PI*r*h<<endl;  
    }  
    void volume()  
    {  
        cout << "该圆柱体的半径为:" << r << "高为:" << h << "体积为:" << PI*r*r*h<< endl;  
    }  
  
};  
  
int main()  
{  
    container *p;  
    cube i(6.0);  
    p = &i;  
    p->surface_area();  
    p->volume();  
    sphere j(5.0);  
    p = &j;  
    p->surface_area();  
    p->volume();  
    cylinder k(5.0,6.5);  
    p = &k;  
    p->surface_area();  
    p->volume();  
    return 0;  
}  

5.15

#include<bits/stdc++.h>
using namespace std;
class area
{
protected:
    double h;
    double w;
public:
    area(double h1,double w1)
    {
        h=h1;
        w=w1;
    }
    virtual double a()=0;
};
class R:public area
{
public:
    R(double h1,double w1):area(h1,w1){}
    double a()
    {
        return 0.5*h*w;
    }
};
int main()
{
    area *p;
    R b(2.0,4.0);
    p=&b;
    cout<<p->a()<<endl;
    return 0;
}

5.16

#include<bits/stdc++.h>
using namespace std;
class T
{
public:
    T(int h1,int m1,int s1)
    {
        h=h1;
        m=m1;
        s=s1;
    }
    void display()
    {
        cout<<"出生时间: "<<h<<"时"<<m<<"分"<<s<<"秒"<<endl;
    }
protected:
    int h,m,s;
};
class D
{
public:
    D(int m1,int d1,int y1)
    {
        m=m1;
        d=d1;
        y=y1;
    }
    void display()
    {
        cout<<"出生年月:"<<y<<"年"<<m<<"月"<<d<<"日"<<endl;
    }
protected:
    int m,d,y;
};
class B:public T,public D
{
    public:
    B(int m1,int d1,int y1,int h1,int mm1,int s1,string name1):T(h1,mm1,s1),D(m1,d1,y1)
    {
        name=name1;
    }
    void display()
    {
        cout<<"姓名 "<<name<<endl;
        T::display();
        D::display();

    }
protected:
    string name;
};
int main()
{
    B a(10,6,1988,1,2,3,"cs");
    a.display();
    return 0;
}

5.17

#include<bits/stdc++.h>
using namespace std;
class T
{
public:
    T(int h1,int m1,int s1)
    {
        h=h1;
        m=m1;
        s=s1;
    }
    void display()
    {
        cout<<"出生时间: "<<h<<"时"<<m<<"分"<<s<<"秒"<<endl;
    }
protected:
    int h,m,s;
};
class D
{
public:
    D(int m1,int d1,int y1)
    {
        m=m1;
        d=d1;
        y=y1;
    }
    void display()
    {
        cout<<"出生年月:"<<y<<"年"<<m<<"月"<<d<<"日"<<endl;
    }
protected:
    int m,d,y;
};
class p
{
public:
    p(string x1,string g1,string z1,long long num1)
    {
        x=x1;
        g=g1;
        z=z1;
        num=num1;
    }
    void display()
    {
        cout<<"性别: "<<x<<endl;
        cout<<"工作部门:"<<g<<endl;
        cout<<"职务: "<<z<<endl;
        cout<<"工资:"<<num<<endl;
    }
protected:
    string x,g,z;
    long long num;
};
class B:public T,public D
{
    public:
    B(int m1,int d1,int y1,int h1,int mm1,int s1,string name1):T(h1,mm1,s1),D(m1,d1,y1)
    {
        name=name1;
    }
    void display()
    {
        cout<<"姓名 "<<name<<endl;
        T::display();
        D::display();

    }
protected:
    string name;
};
class s:public p
{
public:
    s(int m1,int d1,int y1,int h1,int mm1,int s1,string name1,string x1,string g1,string z1,long long num1):ss(m1,d1,y1,h1,mm1,s1,name1),p(x1,g1,z1,num1)
    {

    }
    void display()
    {
        ss.display();
        p::display();
    }
private:
    B ss;

};
int main()
{
    s a(10,6,1988,1,2,3,"cs","女","团委","团委书记",6000);
    a.display();
    return 0;
}

4.24 -----4.25

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
class book
{
public:
    book(int q1)
    {
        q=q1;
        p=q1*10;
    }
    void show()
    {
        cout<<q*p<<endl;
    }

private:
    int q;
    int p;
};
int main()
{
    book s[5]={0,1,2,3,4};
    for(int i=0;i<5;i++)
        s[i].show();

    book *p;
    p=&s[4];
   // s[2].show();
   //逆序输出
    for(int i=4;i>=0;i--)
    {
        p->show();//不是cout<<p->show()的原因
        p--;
    }
    return 0;
}

4.26

#include<bits/stdc++.h>
using namespace std;
class T
{
public:
    T(double p1,int num1)
    {
        p=p1;
        num=num1;
    }
    static double toll;
    void show()
    {
        toll+=p*num;
        //return toll;
    }
private:
    double p;
    int num;
};
double T::toll=0.0;
int main()
{
    T a(2.0,3);
    a.show();
    T b(3.0,2);
    a.show();
    cout<<T::toll<<endl;


    return 0;
}

4.27

#include<bits/stdc++.h>
using namespace std;
class stock
{
public:
    stock(string s1="df",int q1=1000,double p1=8.98)
    {
        s=s1;
        q=q1;
        p=p1;
    }
    void show()
    {
        cout<<this->s<<endl;
        cout<<this->q<<endl;
        cout<<this->p<<endl;
    }
private:
    string s;
    int q;
    double p;

};
int main()
{
    stock c;
    c.show();
    stock a("sdf");
    a.show();
    stock b("dsafdsf",1,2.2);
    b.show();
    return 0;
}

4.28

#include<bits/stdc++.h>
using namespace std;
class shang;
class shen
{
public:
    shen(int g1,int s1,int p1)
    {
        g=g1;
        s=s1;
        p=p1;
    }
    friend void ss(shen a)//既然是友元函数,就不能直接访问g,s,p了

    {
        cout<<a.g+a.s+a.p<<endl;
    }
    friend void sum(shen a,shang b);
    /*
    这是错误的因为 b 是不知道 提前定义了类shang但是也只能yong
    shang而已
     {
        cout<<a.g+a.s+a.p+b.g+b.s+b.p<<endl;
     }
    */

private:
    int g,s,p;

};
class shang
{
public:
    shang(int g1,int s1,int p1)
    {
        g=g1;
        s=s1;
        p=p1;
    }
    void ss()
    {
        cout<<g+s+p<<endl;
    }
    friend void sum(shen a,shang b)//这样写是对的,但是一般写在下面
    {
        cout<<a.g+a.s+a.p+b.g+b.s+b.p<<endl;
    }


private:
    int g,s,p;

};
int main()
{
    shen a(1,2,3);
    shang b(4,5,6);
    ss(a);
    //a.sum();
   // ss(a);
   // b.ss();
   // b.sum();
   sum(a,b);
    return 0;
}
/*
void sum(shen a,shang b)
    {
        cout<<a.g+a.s+a.p+b.g+b.s+b.p<<endl;
    }
*/

4.29

#include<bits/stdc++.h>
using namespace std;
class T
{
public:
    T(string n1,string z1,string c1,int num1,double p1)
    {
        n=n1;
        z=z1;
        c=c1;
        num=num1;
        p=p1;
        sum++;
        sum1+=p;
    }
    void show()
    {
        cout<<n<<" "<<z<<" "<<c<<" "<<num<<" "<<p<<endl;
    }
    // static int sum;
    //static double sum1;
    static int sss()
    {
        return sum;
    }
    static void ss()
    {
        cout<<sum1<<endl;;
    }
private:
    string n,z,c;
    int num;
    double p;
    static int sum;
    static double sum1;

};
int T::sum=0;
double T::sum1=0.0;
int main()
{
    T a("afd","fsdf","dfsf",1,2.0);
    T b("afddfsafd","fsfdsafdf","dewffsf",2,4.0);
    T c("afdfdsaf","fdsfsaffsdf","dfsfdfeaq",3,6.0);
    a.show();
    b.show();
    c.show();
    cout<<T::sss()<<endl;//如果sss()函数不是静态函数这种写法是错误的
    T::ss();
    return 0;
}
//一般用静态成员函数访问静态成员

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值