C++
C#
#include "stdafx.h"
class People2
{
protected: static int staticMember2;
protected: int nonstaticMember2;
public: People2() {} // Must have the line, it implicitly initializes nonstaticMember2
};
class Man2 : public People2
{
public: void setName(People2 x2)
{
printf("%d\n", x2.staticMember2 = 3); // it's ok
printf("%d\n", x2.nonstaticMember2 = 6); // error: inaccessible due to protected
}
};
/******* Above explains inheritance of class, below explains nested class **********/
class People
{
private:
int nonstaticMember;
static int staticMember;
static void test()
{
printf("People Method\n");
}
public:
People() {} // Must have the line, it implicitly initializes nonstaticMember
class Man
{
public:
void setName(People x)
{
printf("%d\n", x.nonstaticMember = 4); // a little weird...
printf("%d\n", People::staticMember = 8);
printf("%d\n", staticMember); // Be the same as People::staticMember
test();
}
};
};
/* The declaration of all static class members must be defined outside all blocks */
int People::staticMember;
int People2::staticMember2;
/* main entry */
void _tmain(int argc, _TCHAR* argv[])
{
/* Nested classes example */
People chinese;
People::Man tom;
tom.setName(chinese);
/* Inheritance example */
People2 japanese;
Man2 eric;
eric.setName(japanese);
}
class People2
{
protected: static int staticMember2;
protected: int nonstaticMember2;
public: People2() {} // Must have the line, it implicitly initializes nonstaticMember2
};
class Man2 : public People2
{
public: void setName(People2 x2)
{
printf("%d\n", x2.staticMember2 = 3); // it's ok
printf("%d\n", x2.nonstaticMember2 = 6); // error: inaccessible due to protected
}
};
/******* Above explains inheritance of class, below explains nested class **********/
class People
{
private:
int nonstaticMember;
static int staticMember;
static void test()
{
printf("People Method\n");
}
public:
People() {} // Must have the line, it implicitly initializes nonstaticMember
class Man
{
public:
void setName(People x)
{
printf("%d\n", x.nonstaticMember = 4); // a little weird...
printf("%d\n", People::staticMember = 8);
printf("%d\n", staticMember); // Be the same as People::staticMember
test();
}
};
};
/* The declaration of all static class members must be defined outside all blocks */
int People::staticMember;
int People2::staticMember2;
/* main entry */
void _tmain(int argc, _TCHAR* argv[])
{
/* Nested classes example */
People chinese;
People::Man tom;
tom.setName(chinese);
/* Inheritance example */
People2 japanese;
Man2 eric;
eric.setName(japanese);
}
C#
using System;
class People2
{
protected static int staticMember2;
protected int nonstaticMember2;
}
class Man2 : People2
{
public void setName(People2 x2)
{
Console.WriteLine("{0}", People2.staticMember2 = 3); // it's ok
Console.WriteLine("{0}", x2.nonstaticMember2 = 6); // error: inaccessible due to private
}
}
/******* Above explains inheritance of class, below explains nested class *****************/
class People
{
private int nonstaticMember;
private static int staticMember;
private static void test()
{
Console.WriteLine("People Method");
}
class Man
{
public void setName(People x)
{
Console.WriteLine("{0}", x.nonstaticMember = 4); // a little weird...
Console.WriteLine("{0}", People.staticMember = 8 );
Console.WriteLine("{0}", staticMember); // Be the same as People.staticMember
test(); // accessible to the contained static class method
}
}
static void Main()
{
/* Nested classes example */
People chinese = new People();
People.Man tom = new People.Man();
tom.setName(chinese);
/* Inheritance example */
People2 japanese = new People2();
Man2 eric = new Man2();
eric.setName(japanese);
}
}
class People2
{
protected static int staticMember2;
protected int nonstaticMember2;
}
class Man2 : People2
{
public void setName(People2 x2)
{
Console.WriteLine("{0}", People2.staticMember2 = 3); // it's ok
Console.WriteLine("{0}", x2.nonstaticMember2 = 6); // error: inaccessible due to private
}
}
/******* Above explains inheritance of class, below explains nested class *****************/
class People
{
private int nonstaticMember;
private static int staticMember;
private static void test()
{
Console.WriteLine("People Method");
}
class Man
{
public void setName(People x)
{
Console.WriteLine("{0}", x.nonstaticMember = 4); // a little weird...
Console.WriteLine("{0}", People.staticMember = 8 );
Console.WriteLine("{0}", staticMember); // Be the same as People.staticMember
test(); // accessible to the contained static class method
}
}
static void Main()
{
/* Nested classes example */
People chinese = new People();
People.Man tom = new People.Man();
tom.setName(chinese);
/* Inheritance example */
People2 japanese = new People2();
Man2 eric = new Man2();
eric.setName(japanese);
}
}