1. Suppose you have two classess A and B and you want to use any of the method of class A inside class B then you have to use (Inheritance)OOPS otherwise you cannot do it.
2. Concept Of friend functions and friend classes comes only when we want to access private or protected or you can say non public( data members and member functions ) of a class in another class.
Friday, July 30, 2010
A Clear Picture for Basic Access Specifers Private,Protected,Public.
First of all keep in mind that a class contains two types of items :-
1. Data Variables
a. Ex: int a , char b like that .
2. Member Functions
a. Ex: int geta(){return a;}
Second thing to keep in mind is we must be very clear with that in which context we are using the acces specifiers:-
Two contexts are there :-
1. Access specifiers used for class items (Data Variables & Member Function ) (i.e. Inside Class Braces)
2. Access specifiers used for class (i.e. Before Class Name )
Now Talking with respect to these contexts:-
1. Access specifiers used for class items (Data Variables & Member Function ) (i.e. Inside Class Braces)
1. Private (Least Scope)
a. Class Data Variables declared as private can be used only by member functions and friends (classes or functions) of the class and cannot be used outside the class otherwise.
b. Class Member Function declared as private can be used by friends (classes or functions) of the class and cannot be used outside the class otherwise.
2. Protected (Same Scope as private + Additional scope for derived classes )
a. Class Data Variables declared as protected can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class. Cannot be used outside the class otherwise.
b. Class Member Function declared as protected can be used by classes derived from the class. Cannot be used outside the class otherwise.
3. Public (Maximum Scope)
a. Class Data Variables declared as public can be used by any function.
b. Class Member Function declared as public can be used anywhere.
Code To Do all The manipulations :- #include
using namespace std;
class BaseClass1 {
public:
int a;
private:
BaseClass1(){}
BaseClass1(int x)
{
a = x;
}
int geta()
{
return a;
}
friend class DerivedClass;
friend int main();
};
class BaseClass2
{
int b;
public:
BaseClass2(int x)
{
b = x;
}
int getb() {
return b=10;
}
};
class DerivedClass : public BaseClass1, public BaseClass2
{
int c;
public :
DerivedClass(int x, int y, int z) : BaseClass1(z), BaseClass2(y)
{
c = BaseClass1::geta();
}
void show()
{
cout << BaseClass1:: geta() << ' ' << getb() << ' ';
cout << c << '\n';
}
};
int main()
{
DerivedClass object(1, 2, 3);
BaseClass1 obj;
int c=obj.geta();
obj.a=10;
object.show();
getch();
return 0;
}
Thursday, July 29, 2010
Scope Of Nested Classes ?
Nested\Inner classes are considered to be within the scope of the Enclosing\Outer class and are available for use within that scope.
The inner class has access to all the methods and fields of the top-level class, even the private ones.
The inner class's short name may not be used outside its scope. If you absolutely must use it, you can use the fully qualified name instead.
Example :-
public class Outer
{
public class Nested
{
public void DisplayA(Outer o) {
System.Console.WriteLine(o.A);
}
}
private int A;
}
To make the object of nested class :-
Outer.Nested n1 = new Outer.Nested();
The inner class has access to all the methods and fields of the top-level class, even the private ones.
The inner class's short name may not be used outside its scope. If you absolutely must use it, you can use the fully qualified name instead.
Example :-
public class Outer
{
public class Nested
{
public void DisplayA(Outer o) {
System.Console.WriteLine(o.A);
}
}
private int A;
}
To make the object of nested class :-
Outer.Nested n1 = new Outer.Nested();
Why Use Nested Classes?
There are several compelling reasons for using nested classes, among them:-
Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Logical grouping of classes—If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
Increased encapsulation—Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
More readable, maintainable code—Nesting small classes within top-level classes places the code closer to where it is used.
Can we declare a class inside a class ? C++\Java\Asp.Net
Yes..
At times you need to define a class only to serve other class and there are no other reasons to make it visible. The best way to implement this is through nested classes. A nested class is a class defined within the scope of another class.
Example:-
class OuterClass
{
...
class NestedClass
{
...
}
}
At times you need to define a class only to serve other class and there are no other reasons to make it visible. The best way to implement this is through nested classes. A nested class is a class defined within the scope of another class.
Example:-
class OuterClass
{
...
class NestedClass
{
...
}
}
Subscribe to:
Posts (Atom)