Nested class

closed account (1vf9z8AR)
I am really struggling with nested classes and i wrote this program but lots of errors since i havent done it before.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
using namespace std;
class student
{
    char name[80];
    int roll;

    class status
    {
        char ch;
    public:
        get();
        out();
    };

public:
    getdata();
    outdata();
};
student st;
status s;
student::get()
{
    cout<<"Alive or Dead(a/d)":cin>>s.ch;
}
student::out()
{
    cout<<"Status-"<<s.ch;
}
student::getdata()
{
    cout<<"Enter name";cin.getline(st.name,80,'.');
    cout<<"Enter roll no";cin>>st.roll;
}
student::outdata()
{
    cout<<"Name-"<<st.name<<endl;
    cout<<"Roll no-"<<st.roll<<endl;
}
int main()
{
    int n,i;
    student st[60];
    status s[60];
    for(i=0;i<n;i++)
    {
        st[i].getdata();
    st[i].get();
        }
            for(i=0;i<n;i++)
            {
                  st[i].outdata();
    st[i].out();
            }

}

error messages-
F:\c++projects\nestedclass\nestedclass.cpp|21|error: 'status' does not name a type|
F:\c++projects\nestedclass\nestedclass.cpp|22|error: no 'int student::get()' member function declared in class 'student'|
F:\c++projects\nestedclass\nestedclass.cpp|26|error: no 'int student::out()' member function declared in class 'student'|
F:\c++projects\nestedclass\nestedclass.cpp||In member function 'int student::getdata()':|
F:\c++projects\nestedclass\nestedclass.cpp|34|warning: no return statement in function returning non-void [-Wreturn-type]|
F:\c++projects\nestedclass\nestedclass.cpp||In member function 'int student::outdata()':|
F:\c++projects\nestedclass\nestedclass.cpp|39|warning: no return statement in function returning non-void [-Wreturn-type]|
F:\c++projects\nestedclass\nestedclass.cpp||In function 'int main()':|
F:\c++projects\nestedclass\nestedclass.cpp|44|error: 'status' was not declared in this scope|
F:\c++projects\nestedclass\nestedclass.cpp|48|error: 'class student' has no member named 'get'|
F:\c++projects\nestedclass\nestedclass.cpp|53|error: 'class student' has no member named 'out'|
||=== Build failed: 6 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

Thankyou
Lines 12-13,22,26: get() and out() are not typed.

Lines 17-18, 30,35: getdata() and outdata() are not typed.

Line 21,44: You can't use status because status is a private subclass of Student.

Line 22: student has no function called get().

Line 24: Statements should be separated by a ;

Line 24,28,32,33,37,38: You're inputting/outputting the global, not the instance data.

Line 26: Ditto regarding out().

Line 20,21: Why are these global? You declare st and s as arrays at line 43-44.

Line 42: n is uninitialized.

Line 45,50: The termination condition of your loop results in undefined behavior because n is uninitialized.

Line 21,44: Why are you declaring s here? status is only declared within student. student should contain an instance of status.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
class student
{
    char name[80];
    int roll;

    class status
    {   char ch;
    public:
        void get();
        void out();
    };

public:
    status  s;
    
    void getdata();
    void outdata();
};

void student::status::get()
{   cout<<"Alive or Dead(a/d)";
    cin >> ch;
}

void student::status::out()
{   cout<<"Status-" << ch;
}

void student::getdata()
{   cout<<"Enter name";
    cin.getline(name,80,'.');
    cout<<"Enter roll no";
    cin >> roll;
}

void student::outdata()
{   cout<<"Name-" << name<<endl;
    cout<<"Roll no-" << roll<<endl;
}

int main()
{   int n,i;
    student st[60];
    
    cout << "How many students?";
    cin >> n;
    for(i=0;i<n;i++)
    {   st[i].getdata();
        st[i].s.get();
    }
    for(i=0;i<n;i++)
    {   st[i].outdata();
        st[i].s.out();
    }

}

Last edited on
Topic archived. No new replies allowed.