Nested class or abstract class

In my opinion there is no special use of nested class as we can do all our works easily and effectively with abstract class in C++.Moreover abstract class gives us more option to improve our code easily later.What are your opinion guys?If there is a single way where nested class is more useful than abstract class?If there is ,please give a short example.I want to see some example where nested class is extremely useful instead of abstract class so that I can understand why it is added in oop.
Are "nested" and "abstract" comparable concepts, or simply apples and oranges?
I am confused,please explain the fact,if you like to give me some time.May be they are similar.May be I am wrong,can you help me to clearify the topic.It would be very helpful for me.
This is a nested class:
1
2
3
4
5
6
class a
{
  class b // Nested class
  {
  };
};
See:
http://en.cppreference.com/w/cpp/language/nested_types

This is an abstract class:
1
2
3
4
class a
{
  virtual void foo() = 0;
};
See:
http://en.cppreference.com/w/cpp/language/abstract_class

How are these concepts comparable?
for abstract class here is a sample code:
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
#include <iostream>
#include<string>
using namespace std;
class person
{
protected:
    string name;
    int id;
public:
    virtual void abstract()=0;
/**some data and function that is declared in "person" class so that other 

child classes can use it.**/

};
class student:public person
{
public:
    void abstract(){};
    void setdata(string s,int id)
    {
        name=s;this->id=id;
    }
    void showdata(){cout<<" "<<name<<" "<<id<<endl;}
/** "student" class used those data of child class*//

};
int main()
{
    student s1;
    s1.setdata("samir",1607073);
    s1.showdata();
    return 0;
}

Here is the same code written with nested class:
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
#include <iostream>
#include<string>
using namespace std;
class student
{
private:
    class data
    {
    public:
    string name;
    int id;
    };
    data d;
public:

    void setdata(string s,int i)
    {
        d.name=s;d.id=i;
    }
    void showdata()
    {
        cout<<d.name<<" "<<d.id<<endl;
    }
};
int main()
{
    student s1;
    s1.setdata("samir",1607073);
    s1.showdata();
    return 0;
}

In my opinion the first system(abstract class) is better.If I upgrade the code later than I/other will use those data+function with other child class .In the second system(nested class) the option of using those particular data is limited to the class "student" only.No other class have any option to use those generalized data.I want to see any case where it would be better if I code with nested class rather than coding with abstract class.
1. You don't make use of inheritance. The (abstract) base class defines an interface. We want to treat all persons as persons. We do not want to know whether they are students or not.

2. What is the difference between abstract and non-abstract class?

3. Nothing forces the 'data' to be nested. Your data has a 'string' member. The 'string' is not nested. Why not inherit from string, if inheritance is so much better?

4. You do "hide" class data, but don't use private inheritance. You could as well use private inheritance and put the class data definition in the public. Your claim that "accessible is better" would then favor the nested class?

In other words, you are comparing apples and oranges and you have arbitrarily chosen a biased case where one seems to be "better".

5. Linked list. List has nodes.
Does the list inherit from node? No.
Is the node abstract? No.
Does the user of list need to access the nodes? No.
Could you nest the node definition? Yes.

6. Have you heard about "pimpl idiom"?
Thanks for your reply.Your comment cleared my confusion and got enough idea about what I was actually searching.I hope with time by practicing more ,I will get more clear idea about their usage and usefulness.Thanks for your time again.
No, I have not heard about pimpl idiom,but after reading your comment i googled it and got some idea.Thanks again.
Last edited on
Topic archived. No new replies allowed.