2
class student
{
private:
    int admno;
    char sname[20];

    class Student_Marks
    {
    private:
        float eng, math, science, computer, Hindi;
        float total;

    public:
        void sMARKS()
        {
            cin >> eng >> math >> science >> computer >> Hindi;
        }

        float cTotal()
        {
            total = eng + math + science + computer + Hindi;
            return total;
        }
    };

public:
    void showData()
    {
        cout << "\n\nAdmission Number :" << admno;
        cout << "\nStudent Name       :" << sname;
        cout << "\nTotal Marks        :" << cTotal();
    }
};

I want to call inner class function cTotal() , in outer class function showData().

I am getting error in accessing inner class function in outer class.

JeJo
  • 30,635
  • 6
  • 49
  • 88

2 Answers2

0

As soon as you call it "nested class" instead of inner class, you might be able to find appropriate references in language guides. It's only a definition of type within enclosing class's scope, you have to create an instance of such class to work with. E.g.

class student
{
    private:
        int admno;
        char sname[20];

    class Student_Marks
    {
        private:
            float eng,math,science,computer,Hindi;
            float total;
        public:
            void sMARKS()
            {
                cout<<"Please enter marks of english,maths,science,computer,science and hindi\n ";
                cin>>eng>>math>>science>>computer>>Hindi;
                
            }
            float cTotal()
            {
                total=eng+math+science+computer+Hindi;
                return total;
            }
    };

    Student_Marks m_marks; // marks of this student

Another problem with your code that your approach to input of input is horribly lacking of error-checking...

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42
0

Your Student_Marks is just a class definition. Without having an object of the Student_Marks class in student, you can not call its member (e.g. cTotal()).

You can have a look at the sample code below:

class student
{
private:
    int admno;
    // better std::string here: what would you do if the name exceeds 20 char?
    char sname[20]; 

    class Student_Marks {
        //  ... code
    };
    Student_Marks student; // create a Student_Marks object in student

public:
    // ...other code!
    void setStudent()
    {
        student.sMARKS();  // to set the `Student_Marks`S members!
    }

    void showData() /* const */
    {
        // ... code
        std::cout << "Total Marks  :" << student.cTotal(); // now you can call the cTotal()
    }
};

Also have a read: Why is "using namespace std;" considered bad practice?

JeJo
  • 30,635
  • 6
  • 49
  • 88