Kênh Tên Miền chuyên cung cấp tên miền đẹp, giá rẻ! Hãy liên hệ kỹ thuật: 0914205579 - Kinh doanh: 0912191357 để được tư vấn, hướng dẫn miễn phí, Cảm ơn quý khách đã ủng hộ trong thời gian qua!
kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Thursday 8 March 2012

Tạo lớp complex (real + image *i ) có các thành phần sau:
- Các thuộc tính : real , image;
- Hàm tạo có sử dụng tham số mặc định
- Nạp chồng các toán tử sau:
+ Toán tử cộng (+) 
+ Toán tử cộng thành phần real của số phức b lên x (b.real + x)
+ Toán tử trừ (-)
+ Toán tử nhập (>>)
+ Toán tử xuất (<<)

#include <iostream.h>
#include <conio.h>
class complex
{
  private:
    float real,image;
  public:
    complex(float r=0,float i=0);
    complex operator+(complex c);
    complex operator-(complex c);
    friend complex operator+(float x,complex c);
    friend ostream& operator<<(ostream &out,complex c);
    friend istream& operator>>(istream &inp,complex& c);
};
 
complex::complex(float r,float i)
{
   real=r;
   image=i;
}
 
complex complex::operator+(complex c)
{
    complex temp;
    temp.real=real+c.real;
    temp.image=image+c.image;
    return temp;
}
 
complex complex::operator-(complex c)
{
   complex temp;
   temp.real=real-c.real;
   temp.image=image-c.image;
   return temp;
}
 
complex operator+(float x, complex c)
{
   complex temp;
   temp.real=c.real+x;
   temp.image=c.image;
   return temp;
}
 
ostream& operator<<(ostream &out,complex c)
{
   cout<<c.real<<"+"<<c.image<<"i"<<endl;
   return out;
}
 
istream& operator>>(istream &inp,complex &c)
{
  cout<<" Nhap vao phan thuc:";
  cin>>c.real;
  cout<<" Nhap vao phan ao:";
  cin>>c.image;
  return inp;
}
 
void main()
{
    complex c,c1,c2,c3;
    float x;
    cin>>c;
    cin>>c1;
    c2=c+c1;
    cout<<" Cong hai so phuc :"<<c2;
    c3=c1-c;
    cout<<" Tru hai so phuc :"<<c3;
    cout<<" Nhap vao x :";
    cin>>x;
    c3=c3+x;
    cout<< c3;
    getch();
}

0 comments:

Post a Comment

domain, domain name, premium domain name for sales

Popular Posts