Classess within Classes

Hi everybody.

I had recently joined B.Tech. Computer Science, and i am doing C++.
I am facing problems while using classess within classes.
Can somebody help me out in this case.

Regards.

Tejinder

Ok I don't know the exact problem you are having but this example might help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//main.cpp
#include <cstdlib>
#include <iostream>
#include "Line.h"

using namespace std;

int main(int argc, char *argv[])
{
    Line testLine = Line();

    testLine.setFirstPoint( 1, 1 );
    testLine.setSecondPoint( 2, 2 );    
    
    testLine.print();
    
    system("PAUSE");
    return EXIT_SUCCESS;
}



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

//line.h

#include <iostream>
#include "point.h"

#define MAX_POINTS  2

#ifndef LINE_H
#define LINE_H

using namespace std;

class Line
{
   public:
      Line();

      void setFirstPoint( int x, int y);
      void setSecondPoint( int x, int y);
      
      void print( void );

   private:      
      Point points[MAX_POINTS];
};

#endif



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
//Line.cpp
#include "Line.h"

Line :: Line()
{
     
}

void Line :: setFirstPoint( int x, int y)
{
     points[0].setX(x);
     points[0].setY(y);     
}

void Line :: setSecondPoint( int x, int y)
{
     points[1].setX(x);
     points[1].setY(y);          
}


void Line :: print( void )
{
     points[0].print();
     points[1].print();     
}



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

//Point.h

#include <iostream>

#ifndef POINT_H
#define POINT_H

using namespace std;

class Point
{
   public:
      Point( int x0 = 0, int y0 = 0);

      void setX( int x0 );
      int getX( void );
      
      int getY( void );
      void setY( int y0 );                  
      
      void print( void );

   private:      
      int x;
      int y;
};

#endif


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
35
36
37
38
//Point.cpp

#include "Point.h"

Point :: Point( int x0, int y0 )
{
   setX(x0);
   setY(y0);   
}


void Point :: setX( int x0 )
{
    x = x0;
}

int Point :: getX( void )
{
   return x;
}

      
int Point :: getY( void )
{
   return y;
}

void Point :: setY( int y0 )
{
    y = y0;
}

void Point :: print( void )
{
     cout << "(" << x << "," << y << ")" << endl;
}




The Program outputs:
(1,1)
(2,2)
is that something related to inner classes ? like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class one
{
           
           class two
           {
                    int a;
                    int b;
           };
           void fun1()
           {
                  two obj1;
                  ....... some blah blah .........
           }
           .............
           ............
};


then these are called as inner classes, they are supported both by java and c++. Inner classes scope only remains within the class which is defining it. If you can what exactly are looking for, then i may provide some detail
Last edited on
Topic archived. No new replies allowed.