-5

I am new in C++ and stuck with this problem. In below code, which statement

cout<<obj.a<<endl;
cout<<obj.b<<endl;
cout<<obj.c<<endl;
cout<<obj.c+obj.a+obj.b<<endl;

prints the output without error?. I am stuck with object creation also. If I create obj as A obj , it also showing error.

#include <iostream>

using namespace std;
main(){
class A
{
    protected:
    int a;
    private:
    float b;
    public:
    double c;
    A(int x,float y,double z)
    {
        a=x;
        b=y;
        c=z;

    }

};
class B: public A
{
    public:
    B(int x,float y,double z): A(x,y,z)
    {

    }

};

class C: public B
{
    public:
    C(int x,float y,double z): B(x,y,z)
    {

    }

};

cout<<obj.a<<endl;
cout<<obj.b<<endl;
cout<<obj.c<<endl;
cout<<obj.c+obj.a+obj.b<<endl;

}
Dayz
  • 269
  • 2
  • 12
  • 2
    Please include the exact error you're seeing. It would also help if you mentioned what you expect the program to print. – jkff Sep 01 '17 at 16:58
  • 1
    Please provide a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). – TriskalJM Sep 01 '17 at 16:58
  • 2
    You don't seem to have declared `obj` at all. – jdehesa Sep 01 '17 at 16:59
  • 1
    main.cpp:4:6: error: ISO C++ forbids declaration of 'main' with no type [-Wpedantic] main(){ – Hariom Singh Sep 01 '17 at 17:10
  • `main` *always* returns `int`. You declaration of `main` is invalid. Also; (as mentioned above) you need to actually declare / instantiate `obj` before you can use it. And `using namespace std;` is going to bite you later. – Jesper Juhl Sep 01 '17 at 17:18
  • You should learn C++. Better Google for C++ tutorials – Asesh Sep 01 '17 at 17:22
  • 1
    @Asesh If you are going to recommend tutorials, direct the asker to good tutorials. Until they have a grasp of what makes good C++, they won't be able to tell the good from the bad. Or the ugly. There is far too much drek out there that will lead the asker down dark paths and teach them to be an even worse programmer to send an asker off on a blind quest. For an asker struggling with material like this, [I'd prefer a good book.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Sep 01 '17 at 17:55

1 Answers1

3

Your code wont work.

  1. You havent declared obj.
  2. You havent instantiated any of the classes.
  3. You cant access private or protected member variables or methods from instantiated objects.

Following example will work:

#include <iostream>

using namespace std;

class A
{
    protected:
        int a;
    private:
        float b;
    public:
        double c;

    A(int x,float y,double z)
    {
        a=x;
        b=y;
        c=z;
    }

    int getA()
    {
        return a;
    }
};

class B: public A
{
    public:
        B(int x,float y,double z): A(x,y,z)
    {
    }
};

class C: public B
{
    public:
        C(int x,float y,double z): B(x,y,z)
    {
    }
};

int main()
{
    C obj(1,1.0,42);

    cout << obj.c << endl;
    cout << obj.getA() << endl;
}
dg.vs
  • 138
  • 1
  • 9