cplus 实验三

#include<iostream>
using namespace std;
class rect{
  public:
      rect(float L=1.0,float W=1.0){
          length=L;
          wide=W;
     }
     area(){
         cout<<length*wide;
     }
 private:
     float length,wide;
 };
 int main(){
     float length,wide;
     cin>>length>>wide;
     rect x(length,wide);
     x.area();
     return 0;
 }

 

#include<iostream>
using namespace std;
class Complex {
      public:
          Complex( double at, double bt) {
              a=at;
              b=bt;
         }
          Complex( double at) {
             a=at;
         }
          Complex(){ a=1; b=1;
    }
         void add(Complex &num) {
             a=a+num.a;
             b=b+num.b;
         }
         void show(){          
   cout<<a<<"+"<<b<<"i"<<endl;
         }
     private:
         double a,b;
        };
 int main(){   
     Complex c1(3,5);
     Complex c2(4.5);
     Complex c3;
     c1.add(c2);
     c1.show();
     c3.show();
     return 0;
   }

实验总结:类与对象是面向对象程序设计最重要的东西,首次接触,不仅需熟知概念性知识,更应多多编写比较,充分理解其宗旨。本次实验涉及到了类,对象。成员,编写时不够熟练顺利。

 

posted @ 2018-04-08 22:08  童梦奇缘  阅读(96)  评论(5)    收藏  举报