this指针、常量和静态成员和友元函数的使用
1、 实验目的和要求:
(1) 掌握显式使用this指针的方法
(2) 掌握静态数据成员的意义及使用方法
(3) 掌握常量数据成员和常量成员函数的意义和使用方法
(4) 掌握友元函数和友元类的使用方法
2、 实验内容:
(1)有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即不用this指针和用this指针)
[cpp]
include <iostream>
using namespace std;
class Myclass
{
public:
Myclass(int a,int b)
{
x=a;
y=b;
}
void copy(Myclass &my);
void print()
{
cout<<"x="<<x<<" ";
cout<<"y="<<y<<endl;
}
private:
int x,y;
};
void Myclass::copy(Myclass &my)
{
this->x=my.x;
this->y=my.y;
}
/*void Myclass::copy(Myclass &my)
{
x=my.x;
y=my.y;
}
*/
int main()
{
Myclass my(10,20),t(30,40);
my.print();
my.copy(t);
my.print();
return 0;
}
#include <iostream>
using namespace std;
class Myclass
{
public:
Myclass(int a,int b)
{
x=a;
y=b;
}
void copy(Myclass &my);
void print()
{
cout<<"x="<<x<<" ";
cout<<"y="<<y<<endl;
}
private:
int x,y;
};
void Myclass::copy(Myclass &my)
{
this->x=my.x;
this->y=my.y;
}
/*void Myclass::copy(Myclass &my)
{
x=my.x;
y=my.y;
}
*/
int main()
{
Myclass my(10,20),t(30,40);
my.print();
my.copy(t);
my.print();
return 0;
}
(2)设计一个类,实现两个复数的四则运算。实现加减乘除功能的函数用友元函数实现。
[cpp]
#include<iostream>
using namespace std;
class Complex
{
private:
double Real;
double Imag;
public:
Complex()
{
Real=0;Imag=0;
}
Complex(double x,double y)
{
Real=x;
Imag=y;
}
Complex(const Complex& c)
{
Real=c.Real;
Imag=c.Imag;
}
void print()
{
cout<<Real<<" + "<<Imag<<" i"<<endl;
}
friend Complex operator +(const Complex c1,const Complex c2);
friend Complex operator -(const Complex c1,const Complex c2);
friend Complex operator *(const Complex c1,const Complex c2);
friend Complex operator /(const Complex c1,const Complex c2);
};
Complex operator +(const Complex c1,const Complex c2)
{
return Complex(c1.Real+c2.Real,c1.Imag+c2.Imag);
}
Complex operator -(const Complex c1,const Complex c2)
{
return Complex(c1.Real-c2.Real,c1.Imag-c2.Imag);
}
Complex operator *(const Complex c1,const Complex c2)
{
return Complex(c1.Real*c2.Real-c1.Imag*c2.Imag,c1.Real*c2.Imag+c2.Real*c1.Imag);
}
Complex operator /(const Complex c1,const Complex c2)
{
double m=c2.Real*c2.Real+c2.Imag*c2.Imag;
return Complex((c1.Real*c2.Real+c1.Imag*c2.Imag)/m,(c1.Imag*c2.Real-c1.Imag*c2.Imag)/m);
}
int main()
{
Complex c1(1,2),c2(3,4);
c1.print();
c2.print();
Complex c3;
c3=c1+c2;
c3.print();
c3=c1-c2;
c3.print();
c3=c1*c2;
c3.print();
c3=c1/c2;
c3.print();
return 0;
}
#include<iostream>
using namespace std;
class Complex
{
private:
double Real;
double Imag;
public:
Complex()
{
Real=0;Imag=0;
}
Complex(double x,double y)
{
Real=x;
Imag=y;
}
Complex(const Complex& c)
{
Real=c.Real;
Imag=c.Imag;
}
void print()
{
cout<<Real<<" + "<<Imag<<" i"<<endl;
}
friend Complex operator +(const Complex c1,const Complex c2);
friend Complex operator -(const Complex c1,const Complex c2);
friend Complex operator *(const Complex c1,const Complex c2);
friend Complex operator /(const Complex c1,const Complex c2);
};
Complex operator +(const Complex c1,const Complex c2)
{
return Complex(c1.Real+c2.Real,c1.Imag+c2.Imag);
}
Complex operator -(const Complex c1,const Complex c2)
{
return Complex(c1.Real-c2.Real,c1.Imag-c2.Imag);
}
Complex operator *(const Complex c1,const Complex c2)
{
补充:软件开发 , C++ ,