I am getting garbage values for this program.. Please help.

/*
===============
== Program 8 ==
===============

    Q. Write a program to enter and print student's details (Personal, Academics
    and Skills) using multiple inheritance
*/

#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>

class skills
{
    public:
    char hobby[];
    char skills[];
};

class academics
{
    public:
   float percent;
};

class student: public skills, public academics
{
    public:
   char NAME[];
   char CLASS[];
   int age;
   char sex;

   void input()
   {
    cout << "Enter the name: ";
       gets(NAME);
       cout << "Enter the class: ";
       gets(CLASS);
       cout << "Enter the age: ";
       cin >> age;
       cout << "Enter the sex (M/F): ";
       cin >> sex;
       cout << "Enter the overall percentage: ";
       cin >> percent;
        cout << "Enter the hobbies (each separated  by a comma): ";
       gets(hobby);
       cout << "Enter the skills (each separated by a comma): ";
       gets(skills);
   }

   void output()
   {
    cout << "Name: " << NAME << endl << "Class: " << CLASS << endl;
      cout << "Age: " << age << endl << "Sex: " << sex << endl;
      cout << "Percent: " << percent << endl << "Hobbies: " << hobby << endl;
      cout << "Skills: " << skills;
   }
};

void main()
{
    student S;
   S.input();
   clrscr();
   S.output();
   int k;
   cin >> k;
}

What size is the array you make like this?
char NAME[];
So how much data can you write into it?

Unsized arrays are allowed in a class, i believe.

Unsized arrays are allowed in a class, i believe.

Only as static members, and even then you must define them elsewhere. Otherwise you're looking at an incomplete type, which is allowed...until you need it to be complete (ie. try to instantiate one).

If you can't give your array a size, you should probably be using a vector.

OK. one more doubt.

#include <iostream.h>
class A
{
    public:
    int x;
};

class B: public A
{
    public:
    int x;
};

int main()
{
    B obj;
    cout << obj.x;
    return 0;
}

How do I use the x inherited from A. Is there an error in the code?

You can resolve the name, but my recommendation is to avoid that situation in the first place if possible. Here's how to resolve the name:

int main()
{
    B obj;
    obj.B::x = 123;
    obj.A::x = 456;
    std::cout << obj.A::x;
    return 0;
}

OK. Thanks!
In my examination, one such question came. It said **find out the syntax errors. **

So, I marked std::cout << obj.x; as an error as x would be ambiguous.

So, that's wrong, and std::cout << obj.x; is not a syntax error. But to use the other x I'll have to write use obj.A::x. Right?

Off the top of my head I want to say that obj.x will use B's copy and A's copy is hidden, but it might be compiled as a syntax error because obj.x is ambiguous. Honestly, I can't recall at the moment which it would be in portable code. :P

Fine. Thanks! :) :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.