脚本宝典收集整理的这篇文章主要介绍了实验一 类与对象,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
实验三
#include <iostream>
#include <cmath>
using namespace std;
class Complex{
private:
double real;
double imag;
public:
Complex();
Complex(double a,double b):real(a),imag(b){}
Complex(Complex &n);
double get_real(Complex c)
{
return real;
}
double get_imag(Complex c)
{
return imag;
}
void show(Complex &c);
void add(Complex &c);
friend Complex add(Complex &c1,Complex &c2);
friend bool is_equal(Complex &c1,Complex &c2);
friend double abs(Complex &c1);
};
void Complex::show(Complex &c)
{
if(c.real==0)
{
if(c.imag!=0)
cout<<c.imag<<"i";
else
cout<<"0";
}
else
{
if(c.imag==0)
cout<<c.real;
if(c.imag>0)
cout<<c.real<<"+"<<c.imag<<"i";
if(c.imag<0)
cout<<c.real<<c.imag<<"i";
}
}
void Complex::add(Complex &c)
{
real+=c.real;
imag+=c.imag;
}
Complex add(Complex &c1,Complex&c2)
{
Complex c3;
c3.real=c2.real+c1.real;
c3.imag=c2.imag+c1.imag;
return c3;
}
bool is_equal(Complex &c1,Complex &c2)
{
if(c1.real==c2.real&&c1.imag==c2.imag)
return 1;
else
return 0;
}
double abs(Complex &c)
{
return sqrt(c.real*c.real+c.imag*c.imag);
}task.3 cpp
#include "Complex.hpp"
#include <iostream>
int main()
{
using namespace std;
Complex c1(3, -4);
const Complex c2(4.5);
Complex c3(c1);
cout << "c1 = ";
c1.show();
cout << endl;
cout << "c2 = ";
c2.show();
cout << endl;
cout << "c2.imag = " << c2.get_imag() << endl;
cout << "c3 = ";
c3.show();
cout << endl;
cout << "abs(c1) = ";
cout << abs(c1) << endl;
cout << boolalpha;
cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
Complex c4;
c4 = add(c1, c2);
cout << "c4 = c1 + c2 = ";
c4.show();
cout << endl;
c1.add(c2);
cout << "c1 += c2, " << "c1 = ";
c1.show();
cout << endl;
}
实验四
#include<iostream>
#include<string>
using namespace std;
class User
{
private:
string name,password,email;
static int n;
public:
User(string x) {name=x; password="111111"; email=''; n++;}
User(string x,string y,string z) {name=x; password=y; email=z; n++;}
void set_email(); void change_passwd();
void print_info();
static void print_n();
};
int User::n=0;
void User::print_n() {
cout<<"there are "<<n<<"users";
}
void User::set_email() {
cout<<"Enter email address:";
string x;
cin>>x;
cout<<"email is set successfully..."<<endl;
}
void User::change_passwd()
{
cout<<"Enter old password: ";
string A,B;
for(int i=1;i<=3;i++)
{
cin>>A;
if(A==password)
{
cout<<"Enter new password: ";
cin>>B;
cout<<"new password is set successfully..."<<endl;
}
else if(i<=2&&A!=password)
{
cout<<"password input error. Please re-enter again: ";
}
else
cout<<"Password input error.Please try after a while."<<endl;
}
}
void User::print_info()
{
cout<<"name: "<<name<<endl;
cout<<"passwd: ******"<<endl;
cout<<"email: " <<email<<endl;
}
//#endif
#include "User.h"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Polaris", "010314", "sct@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}task4.cpp
#include "User.hpp"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Jonny", "92197", "xyz@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}
以上是脚本宝典为你收集整理的实验一 类与对象全部内容,希望文章能够帮你解决实验一 类与对象所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。