hello,
yesterday i was learning inheritance in C++...
My teacher has told me that
"Inheritance means using properties(variable,function etc) of one class to another class."
but when i do it with example its not working..

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

class abc
{
         int a,b,c;
          
         public:
                    void exp1();
};

class xyz:public abc
{
           public:
                     void exp2();
};

void abc::exp1()
{
             cout<<"Enter Value of a= ";
             cin>>a;
             cout<<"Enter Value of b= ";
             cin>>b;
             cout<<"Enter Value of c= ";
             cin>>c;
}

void abc::exp2()
{
             cout<<"a= "<<a;
             cout<<"b= "<<b;
             cout<<"c= "<<c;
}

void main()
{
             clrscr();
             abc a1;
             xyz x1;
             a1.exp1;
             x1.exp2;
             getch();
}

As definition says, i had used the variables of class abc in class xyz, making class abc as public..

so, any problem in my program or in my concept.

Recommended Answers

All 17 Replies

inheritence means something like this: Class B can call the function from class A because class B inherited everything from class A.

class A
{
     void SayHello() { cout << "Hello from class A\n"; }
};

class B : public A
{
public:
   B() { SayHello(); }
}

Ancient Dragon
okay...thanks
but if want to use the variables of class A in Class B, as i had used in my above program than may i??

commented: Stop PMing me with your problems, as I told you many times before -4

Everything is fine except that both of you missed an important point:
private members are not inherited.

And if you know or not, the default access code of a class is private.
Hence the
int a,b,c; in post#1 and the
void SayHello() in post#2 are declared as private.

They cannot be inherited.
Use the protected: access qualifier instead.

This is the corrected version:

#include<iostream>
using std::cout;using std::cin;
class abc
{
        protected: //added a inheritable access specifier
         int a,b,c;
          
         public:
                    void exp1();
};

class xyz: public abc
{
           public:
                     void exp2();
};

void abc::exp1()
{
             cout<<"Enter Value of a= ";
             cin>>a;
             cout<<"Enter Value of b= ";
             cin>>b;
             cout<<"Enter Value of c= ";
             cin>>c;
}

void xyz::exp2() //changed
{
             cout<<"a= "<<a;
             cout<<"b= "<<b;
             cout<<"c= "<<c;
}

int main()
{
          //   clrscr();
             abc a1;
             xyz x1;
             a1.exp1();//added parenthesis in function name
             x1.exp2();//added parenthesis in function name
}

To the OP:
Your code is very rusted. You are using all those deprecated header files, using void main, using getch(). All these are 'crimes' in C++. Please read this guide to migrate to standard C++ It is perhaps because your teacher told you like this. If you are a school goings student, you may write rusted code in school but practice the standard code otherwise.
If you think Daniweb helped you, please help us by using standard C++ and not the crappy turbo C++ accepted C++. Leave Turbo C++ like compilers which are two decades old.

commented: Yes, I should have known better :) +36

siddhant3s..
the program which u had written as a solution, having so many errors...
and as you told that private memer are not inherited i also tried for protected, but it show 2 error
both are like this "Member function must be called or taken inside"
now,
see my final tried program

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
 
class abc
{
		protected:
			int a,b,c;
 
         public:
                    void exp1();
};
 
class xyz:public abc
{
           public:
                     void exp2();
};
 
void abc::exp1()
{
             cout<<"Enter Value of a= ";
             cin>>a;
             cout<<"Enter Value of b= ";
             cin>>b;
             cout<<"Enter Value of c= ";
             cin>>c;
}
 
void xyz::exp2()
{
             cout<<"a= "<<a;
             cout<<"b= "<<b;
             cout<<"c= "<<c;
}
 
int main(void)
{
             clrscr();
             abc a1;
             xyz x1;
             a1.exp1;
             x1.exp2;
             return 0;
}

Who will put the parenthesis, while calling a member function?
Change line 43 and 44 to

a1.exp1();
x1.exp2();

Why does your main function accept the keyword void as an argument?

EDIT:
Wow I'm retarded. Please excuse this.

It is the old crappy rusted style which is embedded in programmers from C background.
In old C,
int fun(void) and int fun() had two different meanings. The former means "accept no arguments" while the latter meant "accept any number of arguments"

In C++, you cannot create a function which can accept "any number of argument" since it support function overloading.
This style of writing main is been deprecated long ago. But people still use it like they still use void main instead of int main though we have told them zillion times not to do so.


Read http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4

Ohh ok. I just thought syntactically that it was incorrect because I just hadn't ever seen int main(void) before. So then why are there compilers in C++ that accept this, even if it conflicts with C++'s overloading abilities? Why would they have designed these compilers (even back in the day) to do this if it is wrong? Perhaps overloading was included in C++ in the future after these crappy old compilers were written? Is that the case?

In C++, you cannot create a function which can accept "any number of argument" since it support function overloading.

I disagree:

#include <cstdarg>
#include <iostream>

using std::cout;

class Test{
public:
    Test(){};
    ~Test(){};
    void Print(int, ...);
    void Print(int,int);
};

void Test::Print(int a,int b){
    cout << a << " " << b << '\n';
}
void Test::Print ( int amount, ...)
{
  va_list vl;
  va_start(vl,amount);
  for (int i=0;i < amount;i++)
  {
    int val=va_arg(vl,int);
    cout << val << " ";
  }
  cout << '\n';
  va_end(vl);
}

int main ()
{
  Test t;
  t.Print (4, 1,2,3,4);
  t.Print (5,6);
}

output:

1 2 3 4 
5 6

Let me state for the record that the above code is horrible and should never be used in real life, because it's just begging for all kinds of undefined behavior. If you want to pass an unknown amount of ints to a function, use a vector.

This is merely to illustrate that a C++ compiler will be happy to compile the code that uses two overloaded functions, with one of them using an unknown amount of arguments.

>I just thought syntactically that it was incorrect
No it isn't. Syntactically it is fine but it is deprecated.
>because I just hadn't ever seen int main(void) before
Just because you have not seen a construct doesn't imply it doesn't exist.

>So then why are there compilers in C++ that accept this,
So as to provide backward compatibility with the old code

> even if it conflicts with C++'s overloading abilities?
It doesn't.

Little correction of post #4.

siddhants>private members are not inherited.

No, all members from base class are added into the declaration space of derived-class(sub class) except constructors.

Private members of base-class are not accesible in sub-class.

Protected members of base-class are accessible in containing class and also sub-class.

>I just thought syntactically that it was incorrect
No it isn't. Syntactically it is fine but it is deprecated.

Just because you don't like a feature or it was inherited from C doesn't make it deprecated. ;) To the best of my knowledge, (void) parameter lists are not deprecated. Please quote chapter and verse from the standard that says it is, because I can't find it and you seem pretty sure of yourself.

@Tom:
http://en.wikipedia.org/wiki/Deprecation
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4
http://www.research.att.com/~bs/sibling_rivalry.pdf point no. 2.5
Specially to quote:

2.5 From Early C++ and C89 to ARM C++
The Annotated C++ Reference Manual [ARM] was published in 1989 and became the base document for
the ANSI C++ standards effort†, starting with its first technical meeting in 1990. Before that, the various
drafts of the ANSI C standard had been available for years and I had been able to take the first steps to
increase C/C++ compatibility. The most important actions were simply to follow the draft C standard
wherever there were no considered decision to do something different. That way, C++ got the C89 rules for
u ns ig ne d, the v ol at il e keyword, etc. The ‘‘abomination’’
un si gn ed vo la ti le
i nt f vo id ;
in t f(v oi d) // f() takes no arguments
was re-incorporated and a redundant comma in declarations of varadic functions introduced into C89 was
also accepted:

@adatapost
Strictly you are right. Private members are 'inherited' but are not accessible to base class members.

#include <iostream>
using std::cout;
class Test{
    int i;
    public:
        Test():i(0){}
        void foo(){i=5;}
        int bar(){return i;}
};

class TestD:public Test{};

int main ()
{
  TestD t;
  t.foo();
  cout<<t.bar();
}

Misunderstanding - Post #10, #11, #13

Read two paragraphs,

int fun(void) and int fun() had two different meanings. The former means "accept no arguments" while the latter meant "accept any number of arguments"

In C++, you cannot create a function which can accept "any number of argument" since it support function overloading.

Paragraph second : If you have an empty argument list, you can declare it as func() in c++, which tells the compiler there are exactly zero arguments.

@Tom:
http://en.wikipedia.org/wiki/Deprecation
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.4
http://www.research.att.com/~bs/sibling_rivalry.pdf point no. 2.5

I won't accept those links. I want you to quote the standard, the only authority on what is or is not deprecated, where it says a parameter list of (void) is deprecated.

It is wrong to say something is deprecated when you really mean 'unconventional'. Deprecated means that the future of that thing is questionable and code that uses it could break anytime in the near future without any changes. That is a real argument for not using a feature, while 'unconventional' just means that readers might stumble over the code.

@adata
The first paragraph is about C, not C++. Clearly I mentioned that.

>If you have an empty argument list, you can declare it as func() in c++, which tells
>the compiler there are exactly zero arguments.
Right. But I never said this is not true. But in C, func() means "unspecified number of arguments".

@Tom
If you wan't to argue that it is not deprecated as 'it will remain in further version of C++', fine.
I agree that this style is not officially declared 'deprecated'. But when an authority such as one of the standard makers (Marshall and Stroustrup) deprecates it, you surely have less choice.
If you just wan't to prove my words wrong..... I really can't help it.
Fine?

(wondering where the OP is)

I agree that this style is not officially declared 'deprecated'. But when an authority such as one of the standard makers (Marshall and Stroustrup) deprecates it, you surely have less choice.

If it's not officially deprecated, then whatever individual 'deprecates' it is only expressing a personal opinion on programming style. You should not propagate misinformation, because 'deprecated' is well known and understood in C++ circles to mean exactly what the standard says it means and nothing else.

If you just wan't to prove my words wrong..... I really can't help it.
Fine?

Fine. I will just keep correcting you when try to mislead people into believing your biased opinions.

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.