C++类的定义 简单的程序 求改错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++类的定义 简单的程序 求改错相关的知识,希望对你有一定的参考价值。

有一处错误,弄不出来,求各位帮改,加注释就好~
#include <iostream>
#include <string>
using namespace std;
class instrument

public:instrument()
virtual void Play()
cout << "instrument" << " is playing !" <<endl;

;
void Print(intrument &ins)
ins.show()

class instrPiano:public instrument
public:
void Play()
cout << "Piano" << " is playing !" << endl;

;
class instrSax:public instrument
public:
void Play()
cout << "Sax" << " is playing !" << endl;

;
class instrFlute:public instrument
public:
void Play()
cout << "Flute" << " is playing !" << endl;

;
void Play(instrument m)
m.Play();

int main()
instrument instr;
instrPiano instrPia;
instrSax instrSax;
instrFlute instrFlu;
Play(instr);
Play(instrPia);
Play(instrSax);
Play(instrFlu);

/*

instrument is playing !

Piano is playing !

Sax is playing !

Flute is playing !

Press any key to continue

*/

#include <iostream>
#include <string>

using namespace std;

class instrument 
public:
instrument()
virtual void Play() 
cout << "instrument" << " is playing !" << endl;

;

void Print(instrument &ins) 
ins.Play(); // ins.show()


class instrPiano:public instrument 
public:
void Play() 
cout << "Piano" << " is playing !" << endl;

;

class instrSax:public instrument 
public:
void Play() 
cout << "Sax" << " is playing !" << endl;

;

class instrFlute:public instrument 
public:
void Play() 
cout << "Flute" << " is playing !" << endl;

;

void Play(instrument &m) 
m.Play();


int main() 
instrument instr;
instrPiano instrPia;
instrSax   instrSax;
instrFlute instrFlu;
Play(instr);
Play(instrPia);
Play(instrSax);
Play(instrFlu);
return 0;

参考技术A void Print(instrument &ins)
ins.Play();
追问

程序是过了 但结果不对额...我是想输出某个乐器是可以play的 但是输出的都是instrument is playing 求亲再帮看看...

追答

void Play(instrument *m)
m->Play();

int main()
instrument instr;
instrPiano instrPia;
instrSax instrSa;
instrFlute instrFlu;
Play(&instr);
Play(&instrPia);
Play(&instrSa);
Play(&instrFlu);
return 0;

理解错了

参考技术B 就是定义Play函数时类名写错了,Print函数中的show函数没有定义,应该改成Play追问

把show改了之后 程序是过了 但结果不对额...我是想输出某个乐器是可以play的 但是输出的都是instrument is playing 求亲再帮看看.

追答

你应该是想用多态性吧?
如果用多态性,先定义一个指向基类的指针,在用这个指针指向不同的派生类,你的那个show函数可以不要的其他的都不用改就把主函数改一下就可以了,我把整个代码给你:
#include
#include
using namespace std;
class instrument

public:
instrument()
virtual void Play()

cout Play();
*/
class instrPiano:public instrument
public:
void Play()

cout Play();
p=&instrPia;
p->Play();
p=&instrSax;
p->Play();
p=&instrFlu;
p->Play();
return 0;

实验2:函数重载函数模板简单类的定义和实现

实验目的 


 

 

1. 掌握c++中函数的声明、定义、调用和参数传递方式

2. 掌握c++中带有默认形参值的函数声明和定义方法

3. 理解函数重载,掌握c++中函数重载的实现方式

4. 理解函数模板,掌握c++中函数模板的简单使用

5. 理解面向对象的抽象和封装,掌握c++中类的定义、实现和使用方法

实验准备


 

 

1. 函数的声明、定义、调用、参数传递方法

2. 带有默认形参值的函数

3. 函数重载

4. 函数模板(9.1.1节 + 9.3节) 其中,9.3节,理解3个常用的排序算法和两个常用的查找算法

5. 类的定义、实现和使用方法

编程内容


 

函数重载编程练习

编写重载函数add(),实现对int型,double型,Complex型数据的加法。在main()函数中定义不同类型 数据,调用测试。(附程序框架,见本文档附1)

函数模板编程练习

编写实现快速排序函数模板,并在main()函数中,定义不同类型数据,调用测试。(算法可参考这里, 内有排序示意图及算法逻辑)

类的定义、实现和使用编程练习

计并实现一个用户类User,并在主函数中使用和测试这个类。具体要求如下: 每一个用户有用户名(name), 密码(passwd),联系邮箱(email)三个属性。 支持设置用户信息setInfo()。允许设置信息时密码默认为6个1,联系邮箱默认为空串。 支持打印用户信息printInfo()。打印用户名、密码、联系邮箱。其中,密码以6个*方式显示。 支持修改密码changePasswd(),。在修改密码前,要求先输入旧密码,验证无误后,才允许修改。 如果输入旧密码时,连续三次输入错误,则提示用户稍后再试,暂时退出修改密码程序。 在main()函数中创建User类实例,测试User类的各项操作(设置用户信息,修改密码,打印用户信 息)

User类功能的完善及拓展丰富(===选做===)

自行设计。在前述内容的基础上,对代码和功能做完善,使其更符合实际应用场景。比如: 支持修改邮箱;用户设置邮箱时对邮箱地址合法性的检测提示(比如是否包含@) 用户更改密码时,当用户输入旧密码和新密码时,屏幕上均以星号代替,而不会显示用户实际 输入的密码;设置密码时对密码的长度、合法性进行有效性校验,等等 。。。

实验结论

 


 

1.重载函数add()

Code:

 

 1 //Add()函数重载
 2 #include<iostream> 
 3 #include<cstdlib>
 4 using namespace std;
 5 struct Complex
 6 {
 7     double real;
 8     double imaginary;
 9 };
10 
11 int add(int, int);
12 double add(double, double);
13 Complex add(Complex, Complex);
14 int main() {
15 
16     int x, y;
17     cout << "Enter two integers:";
18     cin >> x >> y;
19     cout << "Their sum is:" << add(x, y) << endl;
20     //双精度型
21     double m, n;
22     cout << "Enter two decimals:";
23     cin >> m >> n;
24     cout << "Their sum is:" << add(m, n) << endl;
25     //复数
26     Complex k, l;;
27     cout << "Enter two plurals`real part:";
28     cin >> k.real >> l.real;
29     cout << "Enter two plurals`imaginary part:";
30     cin >> k.imaginary >> l.imaginary;
31     cout << "Their sum is:" << add(k, l).real << "+" << add(k, l).imaginary << "i" << endl;
32     system("pause");
33     return 0;
34 }
35 //整型数
36 int add(int a, int b)
37 {
38     int sum;
39     sum = a + b;
40     return sum;
41 }
42 //双精度型数
43 double add(double a, double b)
44 {
45     double sum;
46     sum = a + b;
47     return sum;
48 }
49 //复数
50 Complex add(Complex a, Complex b)
51 {
52     Complex sum;
53     sum.real = a.real + b.real;
54     sum.imaginary = a.imaginary + b.imaginary;
55     return sum;
56 }

 

Screenshot:

技术图片

 

2.快速排序函数模板

main函数:

 

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<ctime>
 4 #include"quicksort.h"
 5 #include"output.h"
 6 using namespace std;
 7 int main()
 8 {
 9     //int
10     int list[50],i,a;
11     cout << "Enter the number(integer) you want to sort(1-50):";
12     cin >> i;
13     cout << "Enter the number:"<<endl;
14     for (a = 0; a <i; a++)
15     {
16         cin >>list[a];
17     }
18     cout << "The list(integer) before sort is:" << endl;
19     output(list, i);
20     cout << "The list(integer) after sorted is:" << endl;
21     quicksort(list, 0, i - 1);
22     output(list, i );
23     //double
24     double listd[50],c;
25     cout << "Enter the number(decimal) you want to sort(1-50):";
26     cin >> i;
27     cout << "Enter the number:" << endl;
28     for (a = 0; a <i; a++)
29     {
30         cin >> listd[a];
31     }
32     cout << "The list(decimal) before sort is:" << endl;
33     output(listd, i);
34     quicksort(listd, 0, i - 1);
35     cout << "The list(decimal) after sorted is:" << endl;
36     output(listd, i);
37     system("pause");
38     return 0;
39 }

 

quicksort函数:

 

 1 #ifndef QUICKSORT_H
 2 #define QUICKSORT_H
 3 template<class T>
 4 void quicksort(T list[], int lo, int hi)
 5 {
 6     if (lo < hi)
 7     {
 8         T x = list[lo];
 9         int a, b = 0, c = 0, jud = 0, k = lo;
10         for (a = 1; a <= hi - lo; a++)
11         {
12             if (jud == 0)
13             {
14                 if (list[hi - b] < x)
15                 {
16                     list[k] = list[hi - b];
17                     list[hi - b] = x;
18                     k = hi - b;
19                     b++;
20                     jud = 1;
21                     continue;
22                 }
23                 else
24                 {
25                     b++;
26                     continue;
27                 }
28             }
29             else
30             {
31                 if (list[lo + c + 1] > x)
32                 {
33                     list[k] = list[lo + c + 1];
34                     list[lo + c + 1] = x;
35                     k = lo + c + 1;
36                     c++;
37                     jud = 0;
38                     continue;
39                 }
40                 else
41                 {
42                     c++;
43                     continue;
44                 }
45             }
46         }
47         quicksort(list, lo, k-1);
48         quicksort(list, k+1, hi);
49     }
50 }
51 #endif

 

output函数:

 

 1 #ifndef OUTPUT_H
 2 #define OUTPUT_H
 3 #include<iostream>
 4 #include<iomanip>
 5 using namespace std;
 6 template<class T>
 7 void output(T list[], int hi)
 8 {
 9     int x,i=0;
10     for (x = 0; x < hi; x++)
11     {
12         i++;
13         cout <<setw(5)<< list[x] << " ";
14         if (i % 6 == 0&&x!=hi-1)
15             cout << endl;
16     }
17     cout <<endl;
18 }
19 #endif

 

Screensort;

技术图片

 

3:用户类User

Code(测试):

 

  1 #include <iostream> 
  2 #include <string> 
  3 #include<cstdlib>
  4 #include<conio.h>
  5 using namespace std;
  6 // User类的声明 
  7 class User
  8 {
  9 public:
 10     void setInfo(string newn, string newp = "111111", string newe = "");
 11     void changePasswd();
 12     void printInfo();
 13     void changeemail();
 14 
 15 private:
 16     string name;
 17     string passwd;
 18     string email;
 19 };
 20 //输入字符串,显示*
 21 string enter()
 22 {
 23     char ch;
 24     string outer;
 25     while ((ch = _getch())!=
)
 26     {
 27         outer += ch;
 28         cout << "*";
 29     }
 30     cout << endl;
 31     return outer;
 32 }
 33 //初始化信息
 34 void User::setInfo(string newn, string newp/*="111111"*/, string newe/*=""*/)
 35 {
 36     name = newn;
 37     passwd = newp;
 38     email = newe;
 39 }
 40 
 41 //修改密码
 42 void User::changePasswd()
 43 {
 44     cout << "Enter the old passwd:";
 45     string outer;
 46     int jud = 1;
 47     outer = enter();
 48     while (outer != passwd)
 49     {
 50         cout << "Passwd input error,please re-enter again:";
 51         outer = enter();
 52         jud++;
 53         if (jud == 3)
 54             break;
 55     }
 56     if (outer != passwd)
 57         cout << "Please try after a while.
";
 58     else
 59     {
 60         cout << "Please enter the new passwd:";
 61         passwd = enter();
 62         do
 63         {
 64             cout << "Please enter the new passwd again:";
 65             outer=enter();
 66             if (outer != passwd)
 67             {
 68                 cout << "Passwd input error,please change the new passwd:";
 69                 passwd = enter();
 70             }
 71             else
 72                 break;
 73         } while (true);
 74         cout << "Finish changepasswd." << endl;
 75     }
 76 }
 77 //修改邮箱
 78 void User::changeemail()
 79 {
 80     string outer,ch;
 81     cout << "Enter the new email:";
 82     cin >> outer;
 83     int a,b,jud;
 84     do
 85     {
 86         jud = 0;
 87         b = outer.length();
 88         for (a = 0; a < b; a++)
 89             if (outer.substr(a, 1) == "@")
 90                 jud++;
 91         if (jud == 0)
 92         {
 93             cout << "Wrong format,please enter again:";
 94             cin >> outer;
 95         }
 96         else
 97             cout << "Finish change email." << endl;
 98     } while (jud == 0);
 99     email = outer;
100 }
101 //打印至屏幕
102 void User::printInfo()
103 {
104     int y,x = passwd.length();
105     string outerx;
106     for (y = 1; y <= x; y++)
107         outerx += *;
108     cout << "name:" << name << endl;
109     cout << "passwd:" << outerx<< endl;
110     cout << "email:" << email << endl;
111 }
112 int main()
113 {
114         cout << "testing 1......" << endl;
115         // 测试1 
116         User user1;
117         user1.setInfo("Leonard");
118         user1.printInfo();
119         user1.changePasswd();
120         user1.printInfo();
121         cout << endl << "testing 2......" << endl << endl;
122         // 测试2
123         User user2;
124         user2.setInfo("Jonny", "92197", "[email protected]");
125         user2.printInfo();
126         system("pause");
127     return 0;
128 }

 

Screensort:

技术图片

 

功能完善

Code:

 

  1 #include <iostream> 
  2 #include <string> 
  3 #include<cstdlib>
  4 #include<conio.h>
  5 using namespace std;
  6 // User类的声明 
  7 class User
  8 {
  9 public:
 10     void setInfo(string newn, string newp = "111111", string newe = "");
 11     void changePasswd();
 12     void printInfo();
 13     void changeemail();
 14 
 15 private:
 16     string name;
 17     string passwd;
 18     string email;
 19 };
 20 //输入字符串,显示*
 21 string enter()
 22 {
 23     char ch;
 24     string outer;
 25     while ((ch = _getch())!=
)
 26     {
 27         outer += ch;
 28         cout << "*";
 29     }
 30     cout << endl;
 31     return outer;
 32 }
 33 //初始化信息
 34 void User::setInfo(string newn, string newp/*="111111"*/, string newe/*=""*/)
 35 {
 36     name = newn;
 37     passwd = newp;
 38     email = newe;
 39 }
 40 
 41 //修改密码
 42 void User::changePasswd()
 43 {
 44     cout << "Enter the old passwd:";
 45     string outer;
 46     int jud = 1;
 47     outer = enter();
 48     while (outer != passwd)
 49     {
 50         cout << "Passwd input error,please re-enter again:";
 51         outer = enter();
 52         jud++;
 53         if (jud == 3)
 54             break;
 55     }
 56     if (outer != passwd)
 57         cout << "Please try after a while.
";
 58     else
 59     {
 60         cout << "Please enter the new passwd:";
 61         passwd = enter();
 62         do
 63         {
 64             cout << "Please enter the new passwd again:";
 65             outer=enter();
 66             if (outer != passwd)
 67             {
 68                 cout << "Passwd input error,please change the new passwd:";
 69                 passwd = enter();
 70             }
 71             else
 72                 break;
 73         } while (true);
 74         cout << "Finish changepasswd." << endl;
 75     }
 76 }
 77 //修改邮箱
 78 void User::changeemail()
 79 {
 80     string outer,ch;
 81     cout << "Enter the new email:";
 82     cin >> outer;
 83     int a,b,jud;
 84     do
 85     {
 86         jud = 0;
 87         b = outer.length();
 88         for (a = 0; a < b; a++)
 89             if (outer.substr(a, 1) == "@")
 90                 jud++;
 91         if (jud == 0)
 92         {
 93             cout << "Wrong format,please enter again:";
 94             cin >> outer;
 95         }
 96         else
 97             cout << "Finish change email." << endl;
 98     } while (jud == 0);
 99     email = outer;
100 }
101 //打印至屏幕
102 void User::printInfo()
103 {
104     int y,x = passwd.length();
105     string outerx;
106     for (y = 1; y <= x; y++)
107         outerx += *;
108     cout << "name:" << name << endl;
109     cout << "passwd:" << outerx<< endl;
110     cout << "email:" << email << endl;
111 }
112 int main()
113 {
114     User you;
115     //输入用户名
116     string name;
117     cout << "Enter your name(password:111111):";
118     cin >> name;
119     you.setInfo(name);
120     //操作列表
121     cout << "Choose your operation" << endl;
122     cout << "1--change password"<<endl;
123     cout << "2-change email"<<endl;
124     cout << "3-print on the screen" << endl;
125     cout << "0-EXIT" << endl;
126     int x=1;
127     while (x!=0)
128     {
129         cout << "Your operation is:";
130         cin >> x;
131         switch (x)
132         {
133         case 1:
134             you.changePasswd();
135             continue;
136         case 2:
137             you.changeemail();
138             continue;
139         case 3:
140             you.printInfo();
141             continue;
142         default:
143             cout<<"Wrong operation,please enter again."<<endl;
144         }
145     }
146     system("pause");
147     return 0;
148 }

 

 

Screensort:

 技术图片

 

实验总结与体会

 


 

1.写预处理命令时记得大写

2.写快速排序时注意数组从零开始

3.实验一中单独建立一个复数类会更方便计算

4.在一定限度内,快速排序速度最快,但当数据过大时,就会占用过多内存,这时用其他的排序法会更好

5.实验三中单独建立一个声明User类的头文件会使代码可读性更高,更加方便查找bug并修改

6.函数重载减少了函数名的数量,使程序更加面向对象化

以上是关于C++类的定义 简单的程序 求改错的主要内容,如果未能解决你的问题,请参考以下文章

c++程序改错了 谢谢

实验2:函数重载函数模板简单类的定义和实现

C++用面向对象的方法求阶乘

一个简单的c++类的定义和实例化

如何用C++实现一个整数类的集合??

C++类的组合和聚合