| |

VerySource

 Forgot password?
 Register
Search
View: 1491|Reply: 9

Wait online ~~

[Copy link]

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-2 19:00:01
| Show all posts |Read mode
#include <iostream.h>
#include <string.h>
#define MAX 2
class student
{
private:
char name [20];
char reseach [20];
int no;
public:
        student (char n [], char s [], int t)
         {
            strcpy (name, n);
            strcpy (reseach, s);
            no = t;
         }
~ student () {}
void ShowInfo ()
{
cout << "student:" << name;
                cout << "Research direction:" << reseach;
                cout << "Student ID:" << no;
}
};
class teacher
{
private:
int top;
char name [20];
student stu [MAX];
public:
        teacher (char t [])
{
top = 0;
strcpy (name, t);
}
        ~ teacher ()
{
          // delete [] stu;
}
void add (student&s)
{
           stu [top] = s;
top ++;
}
void getname ()
{
cout << "Teacher:" << name << endl;
}
void ShowStudentInfo ()
{
for (int i = 0; i <top; i ++)
{
            stu [i] .ShowInfo ();
}
}
};
void main ()
{
teacher t [] = {teacher ("Tom"), teacher ("Marry")};
student s1 = ("Pirlo", "Java", 100);
student s2 = ("Gattuso", "C ++", 101);
student s3 = ("kaka", "C", 102);
t [0] .add (s1);
t [0] .add (s2);
t [1] .add (s3);
             for (int i = 0; i <2; i ++)
{
t [i] .getname ();
cout << "Guiding students:";
           t [i] .ShowStudentInfo ();
}
}



D:\C ++ job\and 2\zuoye1.cpp (32): error C2512: 'student': no ​​appropriate default constructor available
D:\C ++ homework\and 2\zuoye1.cpp (61): error C2440: 'initializing': cannot convert from 'const int' to 'class student'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C ++ Assignment\and 2\zuoye1.cpp (62): error C2440: 'initializing': cannot convert from 'const int' to 'class student'
        No constructor could take the source type, or constructor overload resolution was ambiguous
D:\C ++ homework\and 2\zuoye1.cpp (63): error C2440: 'initializing': cannot convert from 'const int' to 'class student'
        No constructor could take the source type, or constructor overload resolution was ambiguous
Error executing cl.exe.

zuoye1.obj-4 error (s), 0 warning (s)
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-5-14 01:00:01
| Show all posts
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
#define MAX 2
class student
{
private:
char name [20];
char reseach [20];
int no;
public:
        student (char n [], char s [], int t)
         {
            strcpy (name, n);
            strcpy (reseach, s);
            no = t;
         }
~ student () {}
student ()
{

}
void ShowInfo ()
{
cout << "Student:" << name;
                cout << "Research direction:" << reseach;
                cout << "Student ID:" << no;
}
};
class teacher
{
private:
int top;
char name [20];
student stu [MAX];
public:
        teacher (char t [])
{
top = 0;
strcpy (name, t);
}
        ~ teacher ()
{
          // delete [] stu;
}
void add (student&s)
{
           stu [top] = s;
top ++;
}
void getname ()
{
cout << "Teacher:" << name << endl;
}
void ShowStudentInfo ()
{
for (int i = 0; i <top; i ++)
{
            stu [i] .ShowInfo ();
}
}
};
void main ()
{
teacher t [] = {teacher ("Tom"), teacher ("Marry")};
student s1 ("Pirlo", "Java", 100);
student s2 ("Gattuso", "C ++", 101);
student s3 ("kaka", "C", 102);
t [0] .add (s1);
t [0] .add (s2);
t [1] .add (s3);
             for (int i = 0; i <2; i ++)
{
t [i] .getname ();
cout << "Guide students:";
           t [i] .ShowStudentInfo ();
}
}
Reply

Use magic Report

0

Threads

12

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 United States

Post time: 2020-5-14 11:45:01
| Show all posts
I have defined the constructor myself. Be careful to add a default constructor. The function I gave is empty. You can change it according to your needs.
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-5-15 18:15:02
| Show all posts
#include <iostream>
#include <string>
using namespace std;

#define MAX 2
class student
{
private:
char name [20];
char reseach [20];
int no;
public:
student ()
{
strcpy (name, "");
strcpy (reseach, "");
no = 0;
}
        student (char n [], char s [], int t)
         {
            strcpy (name, n);
            strcpy (reseach, s);
            no = t;
         }
~ student () {}
void ShowInfo ()
{
cout << "name:" << name;
                cout << "reseach:" << reseach;
                cout << "no:" << no;
}
};
class teacher
{
private:
int top;
char name [20];
student stu [MAX];
public:
teacher ()
{
top = 0;
strcpy (name, "");
}
        teacher (char t [])
{
top = 0;
strcpy (name, t);
}
        ~ teacher ()
{
          // delete [] stu;
}
void add (student&s)
{
           stu [top] = s;
top ++;
}
void getname ()
{
cout << "teacher:" << name << endl;
}
void ShowStudentInfo ()
{
for (int i = 0; i <top; i ++)
{
            stu [i] .ShowInfo ();
}
}
};
int main ()
{
teacher t [] = {teacher ("Tom"), teacher ("Marry")};
student s1 ("Pirlo", "Java", 100);
student s2 ("Gattuso", "C ++", 101);
student s3 ("kaka", "C", 102);
t [0] .add (s1);
t [0] .add (s2);
t [1] .add (s3);
             for (int i = 0; i <2; i ++)
{
t [i] .getname ();
cout << "student:";
           t [i] .ShowStudentInfo ();
}
The
 return 0;
}
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-5-16 13:45:01
| Show all posts
1. This is wrong.
teacher (char t [])

2. This will construct MAX students. If the construction fails, take a look at the student constructor.
student stu [MAX].
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-20 16:45:01
| Show all posts
The first error is that there is no default constructor, I added it, but there are three errors behind it
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-5-21 15:45:01
| Show all posts
teacher (char t [])
Please tell me why this is wrong?
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-5-21 20:30:01
| Show all posts
Put the wrong code places, one by one to help you solve it, so put it all at once, your head is dizzy
Reply

Use magic Report

2

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-22 12:30:02
| Show all posts
These are the few, the original code is at the top,


student s1 ("Pirlo", "Java", 100);
student s2 ("Gattuso", "C ++", 101);
student s3 ("kaka", "C", 102);
Error message see above
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-6-13 18:00:01
| Show all posts
#include<iostream.h>
#include<string.h>

#define MAX 2

class student
{
public:
student()
{
}
The
student(char n[],char s[],int t)
{
strcpy(name,n);
strcpy(reseach,s);
no=t;
}

~student()
{
}

void ShowInfo()
{
cout << "Student: "<< name << ""
<< "Research direction: "<< reseach << ""
<< "Student ID: "<< no << ""
<< endl;
}


private:
char name[20];
char reseach[20];
int no;
};

class teacher
{
private:
int top;
char name[20];
student stu[MAX];
public:

teacher(char t[])
{
top=0;
strcpy(name,t);
}

~teacher()
{
// delete []stu;
}

void add(student&s)
{
stu[top]=s;
top++;
}

void getname()
{
cout<<"Teacher:"<<name<<endl;
}

void ShowStudentInfo()
{
for(int i=0;i<top;i++)
{
stu[i].ShowInfo();
cout << endl;
}
}
};
void main()
{
teacher t[]={teacher("Tom"),teacher("Marry")};
// student("Pirlo","Java",100);
// student("Gattuso","C++",101)
// student("kaka","C",102)
t[0].add(student("Pirlo","Java",100));
t[1].add(student("Gattuso","C++",101));
t[1].add(student("kaka","C",102));
for(int i=0;i<2;i++)
{
t[i].getname();
cout<< endl;
t[i].ShowStudentInfo();
cout << endl;
}
}
This looks okay, but it's a bit messy, let's check it out again!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list