task2:array<int,5> x3<x1>;语句在devc的环境下可能会报错,改成array<int,5> x3(x1)可正确编译。
task3:to_string的功能是将数字常量转换为字符串,还有就是要注意,静态变量类外初始化,并且要注意初始化时没有static。
task4:
#pragma once #include<iostream> #include<cmath> using namespace std; class Complex{ public: Complex (double x=0.0,double y=0.0 ); Complex(const Complex &c); double get_real() const; double get_imag() const; void show() const; void add(Complex c1); friend Complex add(Complex c1,Complex c2); friend bool is_equal(Complex c1,Complex c2); friend double abs(Complex c); private: double real; double imag; }; Complex::Complex(double x,double y) :real(x),imag(y){} Complex:: Complex (const Complex &c):real(c.real),imag(c.imag){} double Complex :: get_real() const{return real;} double Complex :: get_imag() const{return imag;} void Complex::add(Complex c1){real=real+c1.real;imag=imag+c1.imag;} void Complex::show() const {if(imag>0)cout<<real<<"+"<<imag<<"i";else if(imag<0) cout<<real<<imag<<"i";else cout<<real;} Complex add(Complex c1,Complex c2){Complex c3;c3.real=c1.real+c2.real;c3.imag=c1.imag+c2.imag;return c3;} bool is_equal(Complex c1,Complex c2){if(c1.real==c2.real&&c1.imag==c2.imag) return true ;else return false;} double abs(Complex c){double s=sqrt(c.real*c.real+c.imag*c.imag); return s;}
#include "Complex.h" #include <iostream> void test() { 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; } int main() { test(); }
出现的问题:实验四的.cpp文件中用const定义了一个对象c2,所以c2.show()必须也是const,因为c2这个对象是无权访问非const成员函数的。并且,构造函数是不能通过const进行重载的!
task5:
#ifndef ABCD_H #define ABCD_H #include<iostream> #include<string> using namespace std; class User{ public: User (string name0,string passwd1="111111",string email1="\0"); User(const User&u) :name{ u.name }, passwd{ u.passwd }, email{ u.email }{ User::n++;} ~User(); void set_email(); void change_passwd(); void print_info(); static void printf_n(){cout<<"there are"<<User::n<<"usrs"<<endl; } private : string name; string passwd; string email; static int n; }; int User::n=0;
User::User(string name0,string passwd1,string email1):name{name0},passwd{passwd1},email(email1){User::n++;}
User::~User(){User::n--;} void User::set_email(){email="\0";cout<<"enter email adress:"; cin>>email;cout<<"email is set successfully..."<<endl;} void User::change_passwd(){ int i=0,flag=0; cout<<"enter old passwd"<<endl; string p1; cin>>p1; for(i=0;i<=2;i++) { if(p1!=passwd) { cout<<"send error,please write again"; cin>>p1;} else { string p2; cout<<"enter new password:"; cin>>p2; passwd=p2; cout<<"new password is set successfully"<<endl; flag=1; break;}} if(flag==0) cout<<"please try after a while"<<endl; } void User::print_info() {int i=0; cout<<"name"<<name<<endl; for(i=0;i<passwd.size();i++) cout<<"*"; cout<<"email:"<<email<<endl; } //cin不能有endl //字符串的赋值 #endif
task5.cpp:
#include "User.h"
#include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Jonny", "92197", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::printf_n(); } int main() { test(); }
task5存在的问题:cin输入里不能有endl控制符,会报错。还有就是对string字符串类型的赋值要注意
标签:const,对象,double,void,Complex,实验,User,imag From: https://www.cnblogs.com/xiaozhengcaicai/p/16795666.html