码迷,mamicode.com
首页 > 其他好文 > 详细

实验3类与对象

时间:2018-04-03 19:22:19      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:oid   面积   对象   complex   gpo   idt   std   div   技术   

4-11

#include<iostream> 
using namespace std; 
class Rectangle{
    public:
        Rectangle(float length,float width);
    private:
        float l;
        float w; 
};
Rectangle::Rectangle(float length,float width){
    l=length;
    w=width;
}
int main(){
    float l;
    float w;
    cout<<"请输入长:";
    cin>>l;
    cout<<"请输入宽:";
    cin>>w;
    cout<<"面积为:"<<l*w<<endl;
    return 0;
}

技术分享图片

4-20 

#include<iostream>
using namespace std;
class Complex{
 float c,d;
public:
 Complex(float a=0,float b=0){c=a;d=b;}
 void add(Complex b);
    void show();
};
void Complex::add(Complex b){
 c+=b.c;
 d+=b.d;
}
void Complex::show(){
 cout<<"c="<<c<<"+"<<d<<"i"<<endl;
}
int main(){
 Complex c1(3,5);
 Complex c2=4.5;
 c1.add(c2);
 c1.show();
 return 0;
}

技术分享图片

 

实验3类与对象

标签:oid   面积   对象   complex   gpo   idt   std   div   技术   

原文地址:https://www.cnblogs.com/zszssy/p/8710189.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!