From: ..::ISHFAQ::.. <mc100402092@vu.edu.pk>
Date: Sat, May 7, 2011 at 3:58 PM
Subject: CS304_Example_inheritance
To: , afaaq <afaaqtariq233@gmail.com>,
Friends are functions or classes declared with the
If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:
The duplicate function is a friend of CRectangle. From within that function we have been able to access the members width andheight of different objects of type CRectangle, which are private members. Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle. It isn't! It simply has access to its private and protected members without being a member.
The friend functions can serve, for example, to conduct operations between two different classes. Generally, the use of friend functions is out of an object-oriented programming methodology, so whenever possible it is better to use members of the same class to perform operations with them. Such as in the previous example, it would have been shorter to integrate duplicate()within the class CRectangle.
In this example, we have declared CRectangle as a friend of CSquare so that CRectangle member functions could have access to the protected and private members of CSquare, more concretely to CSquare::side, which describes the side width of the square.
You may also see something new at the beginning of the program: an empty declaration of class CSquare. This is necessary because within the declaration of CRectangle we refer to CSquare (as a parameter in convert()). The definition of CSquare is included later, so if we did not include a previous empty declaration for CSquare this class would not be visible from within the definition of CRectangle.
Consider that friendships are not corresponded if we do not explicitly specify so. In our example, CRectangle is considered as a friend class by CSquare, but CRectangle does not consider CSquare to be a friend, so CRectangle can access the protected and private members of CSquare but not the reverse way. Of course, we could have declared also CSquare as friend of CRectangle if we wanted to.
Another property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.
This could be represented in the world of classes with a class CPolygon from which we would derive the two other ones:CRectangle and CTriangle.
The class CPolygon would contain members that are common for both types of polygon. In our case: width and height. AndCRectangle and CTriangle would be its derived classes, with specific features that are different from one type of polygon to the other.
Classes that are derived from others inherit all the accessible members of the base class. That means that if a base class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B.
In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format:
class derived_class_name: public base_class_name
{ /*...*/ };
Where derived_class_name is the name of the derived class and base_class_name is the name of the class on which it is based. Thepublic access specifier may be replaced by any one of the other access specifiers protected and private. This access specifier limits the most accessible level for the members inherited from the base class: The members with a more accessible level are inherited with this level instead, while the members with an equal or more restrictive access level keep their restrictive level in the derived class.
The objects of the classes CRectangle and CTriangle each contain members inherited from CPolygon. These are: width, height andset_values().
The protected access specifier is similar to private. Its only difference occurs in fact with inheritance. When a class inherits from another one, the members of the derived class can access the protected members inherited from the base class, but not its private members.
Since we wanted width and height to be accessible from members of the derived classes CRectangle and CTriangle and not only by members of CPolygon, we have used protected access instead of private.
We can summarize the different access types according to who can access them in the following way:
Where "not members" represent any access from outside the class, such as from main(), from another class or from a function.
In our example, the members inherited by CRectangle and CTriangle have the same access permissions as they had in their base class CPolygon:
This is because we have used the public keyword to define the inheritance relationship on each of the derived classes:
This public keyword after the colon (:) denotes the most accessible level the members inherited from the class that follows it (in this case CPolygon) will have. Since public is the most accessible level, by specifying this keyword the derived class will inherit all the members with the same levels they had in the base class.
If we specify a more restrictive access level like protected, all public members of the base class are inherited as protected in the derived class. Whereas if we specify the most restricting of all access levels: private, all the base class members are inherited as private.
For example, if daughter was a class derived from mother that we defined as:
This would set protected as the maximum access level for the members of daughter that it inherited from mother. That is, all members that were public in mother would become protected in daughter. Of course, this would not restrict daughter to declare its own public members. That maximum access level is only set for the members inherited from mother.
If we do not explicitly specify any access level for the inheritance, the compiler assumes private for classes declared with classkeyword and public for those declared with struct.
Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.
If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class:
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
For example:
Notice the difference between which mother's constructor is called when a new daughter object is created and which when it is ason object. The difference is because the constructor declaration of daughter and son:
here is the complete example:
Best Wishes,
Date: Sat, May 7, 2011 at 3:58 PM
Subject: CS304_Example_inheritance
To: , afaaq <afaaqtariq233@gmail.com>,
Friendship and inheritance
Friend functions
In principle, private and protected members of a class cannot be accessed from outside the same class in which they are declared. However, this rule does not affect friends.Friends are functions or classes declared with the
friend
keyword.If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:
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 |
| 24 |
The duplicate function is a friend of CRectangle. From within that function we have been able to access the members width andheight of different objects of type CRectangle, which are private members. Notice that neither in the declaration of duplicate() nor in its later use in main() have we considered duplicate a member of class CRectangle. It isn't! It simply has access to its private and protected members without being a member.
The friend functions can serve, for example, to conduct operations between two different classes. Generally, the use of friend functions is out of an object-oriented programming methodology, so whenever possible it is better to use members of the same class to perform operations with them. Such as in the previous example, it would have been shorter to integrate duplicate()within the class CRectangle.
Friend classes
Just as we have the possibility to define a friend function, we can also define a class as friend of another one, granting that first class access to the protected and private members of the second one.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 |
| 16 |
In this example, we have declared CRectangle as a friend of CSquare so that CRectangle member functions could have access to the protected and private members of CSquare, more concretely to CSquare::side, which describes the side width of the square.
You may also see something new at the beginning of the program: an empty declaration of class CSquare. This is necessary because within the declaration of CRectangle we refer to CSquare (as a parameter in convert()). The definition of CSquare is included later, so if we did not include a previous empty declaration for CSquare this class would not be visible from within the definition of CRectangle.
Consider that friendships are not corresponded if we do not explicitly specify so. In our example, CRectangle is considered as a friend class by CSquare, but CRectangle does not consider CSquare to be a friend, so CRectangle can access the protected and private members of CSquare but not the reverse way. Of course, we could have declared also CSquare as friend of CRectangle if we wanted to.
Another property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.
Inheritance between classes
A key feature of C++ classes is inheritance. Inheritance allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own. For example, we are going to suppose that we want to declare a series of classes that describe polygons like our CRectangle, or like CTriangle. They have certain common properties, such as both can be described by means of only two sides: height and base.This could be represented in the world of classes with a class CPolygon from which we would derive the two other ones:CRectangle and CTriangle.
The class CPolygon would contain members that are common for both types of polygon. In our case: width and height. AndCRectangle and CTriangle would be its derived classes, with specific features that are different from one type of polygon to the other.
Classes that are derived from others inherit all the accessible members of the base class. That means that if a base class includes a member A and we derive it to another class with another member called B, the derived class will contain both members A and B.
In order to derive a class from another, we use a colon (:) in the declaration of the derived class using the following format:
class derived_class_name: public base_class_name
{ /*...*/ };
Where derived_class_name is the name of the derived class and base_class_name is the name of the class on which it is based. Thepublic access specifier may be replaced by any one of the other access specifiers protected and private. This access specifier limits the most accessible level for the members inherited from the base class: The members with a more accessible level are inherited with this level instead, while the members with an equal or more restrictive access level keep their restrictive level in the derived 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 32 33 |
| 20 10 |
The objects of the classes CRectangle and CTriangle each contain members inherited from CPolygon. These are: width, height andset_values().
The protected access specifier is similar to private. Its only difference occurs in fact with inheritance. When a class inherits from another one, the members of the derived class can access the protected members inherited from the base class, but not its private members.
Since we wanted width and height to be accessible from members of the derived classes CRectangle and CTriangle and not only by members of CPolygon, we have used protected access instead of private.
We can summarize the different access types according to who can access them in the following way:
Access | public | protected | private |
---|---|---|---|
members of the same class | yes | yes | yes |
members of derived classes | yes | yes | no |
not members | yes | no | no |
Where "not members" represent any access from outside the class, such as from main(), from another class or from a function.
In our example, the members inherited by CRectangle and CTriangle have the same access permissions as they had in their base class CPolygon:
1 2 3 4 5 |
|
This is because we have used the public keyword to define the inheritance relationship on each of the derived classes:
|
This public keyword after the colon (:) denotes the most accessible level the members inherited from the class that follows it (in this case CPolygon) will have. Since public is the most accessible level, by specifying this keyword the derived class will inherit all the members with the same levels they had in the base class.
If we specify a more restrictive access level like protected, all public members of the base class are inherited as protected in the derived class. Whereas if we specify the most restricting of all access levels: private, all the base class members are inherited as private.
For example, if daughter was a class derived from mother that we defined as:
|
This would set protected as the maximum access level for the members of daughter that it inherited from mother. That is, all members that were public in mother would become protected in daughter. Of course, this would not restrict daughter to declare its own public members. That maximum access level is only set for the members inherited from mother.
If we do not explicitly specify any access level for the inheritance, the compiler assumes private for classes declared with classkeyword and public for those declared with struct.
What is inherited from the base class?
In principle, a derived class inherits every member of a base class except:- its constructor and its destructor
- its operator=() members
- its friends
Although the constructors and destructors of the base class are not inherited themselves, its default constructor (i.e., its constructor with no parameters) and its destructor are always called when a new object of a derived class is created or destroyed.
If the base class has no default constructor or you want that an overloaded constructor is called when a new derived object is created, you can specify it in each constructor definition of the derived class:
derived_constructor_name (parameters) : base_constructor_name (parameters) {...}
For example:
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 |
| mother: no parameters daughter: int parameter mother: int parameter son: int parameter |
Notice the difference between which mother's constructor is called when a new daughter object is created and which when it is ason object. The difference is because the constructor declaration of daughter and son:
1 2 |
|
Multiple inheritance
In C++ it is perfectly possible that a class inherits members from more than one class. This is done by simply separating the different base classes with commas in the derived class declaration. For example, if we had a specific class to print on screen (COutput) and we wanted our classes CRectangle and CTriangle to also inherit its members in addition to those of CPolygon we could write:1 2 | |
here is the complete example:
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 39 40 41 42 |
|
Best Wishes,
"...SUBHANALLAHI WABI HAMDIHI...SUBHANALLAH IL AZEEM..."
Muhammd Ishfaq
MCS 2nd Semester (PakPattan)
Muhammd Ishfaq
MCS 2nd Semester (PakPattan)
---------- Forwarded message ----------
From: ..::ISHFAQ::.. <mc100402092@vu.edu.pk>
Date: Sat, May 7, 2011 at 3:53 PM
Subject: ::::|| VU ||:::: CS304_Example_inheritance
To: "hafiz.salman.majeed1" <hafiz.salman.majeed1@gmail.com>, CoooL <asad.coool786@gmail.com>, Miss Kazmi <mc100402020@vu.edu.pk>, Muhammad Aftab <mc100202044@vu.edu.pk>, Muhammad Soban <mc090410137@vu.edu.pk>, afaaq <afaaqtariq233@gmail.com>, coool_vu_students <coool_vu_students@googlegroups.com>, vustudymania <vustudymania@googlegroups.com>, zavia-lms <zavia-lms@googlegroups.com>
In general, a well-designed class will have no public fields--everything should go through the class's functions. Functions that retrieve variables are known as 'getters' and those that change values are known as 'setters'. Since the public part of the class is intended for use by others, it is often sensible to put the public section at the top of the class.
Best Wishes,
From: ..::ISHFAQ::.. <mc100402092@vu.edu.pk>
Date: Sat, May 7, 2011 at 3:53 PM
Subject: ::::|| VU ||:::: CS304_Example_inheritance
To: "hafiz.salman.majeed1" <hafiz.salman.majeed1@gmail.com>, CoooL <asad.coool786@gmail.com>, Miss Kazmi <mc100402020@vu.edu.pk>, Muhammad Aftab <mc100202044@vu.edu.pk>, Muhammad Soban <mc090410137@vu.edu.pk>, afaaq <afaaqtariq233@gmail.com>, coool_vu_students <coool_vu_students@googlegroups.com>, vustudymania <vustudymania@googlegroups.com>, zavia-lms <zavia-lms@googlegroups.com>
اسلام علیکم ورحمتہ اللہ و برکاتہ
A quick example of inheritance:class Animal { public: Animal(); ~Animal(); void eat(); void sleep(); void drink(); private: int legs; int arms; int age; }; //The class Animal contains information and functions //related to all animals (at least, all animals this lesson uses) class Cat : public Animal { public: int fur_color; void purr(); void fish(); void markTerritory(); }; //each of the above operations is unique //to your friendly furry friends //(or enemies, as the case may be)A discussion of the keywords public, private, and protected is useful when discussing inheritance. The three keywords are used to control access to functions and variables stored within a class.
public:
The most open level of data hiding is public. Anything that is public is available to all derived classes of a base class, and the public variables and data for each object of both the base and derived class is accessible by code outside the class. Functions marked public are generally those the class uses to give information to and take information from the outside world; they are typically the interface with the class. The rest of the class should be hidden from the user using private or protected data (This hidden nature and the highly focused nature of classes is known collectively as encapsulation). The syntax for public is:public:Everything following is public until the end of the class or another data hiding keyword is used.
In general, a well-designed class will have no public fields--everything should go through the class's functions. Functions that retrieve variables are known as 'getters' and those that change values are known as 'setters'. Since the public part of the class is intended for use by others, it is often sensible to put the public section at the top of the class.
protected:
Variables and functions marked protected are inherited by derived classes; however, these derived classes hide the data from code outside of any instance of the object. Keep in mind, even if you have another object of the same type as your first object, the second object cannot access a protected variable in the first object. Instead, the second object will have its own variable with the same name - but not necessarily the same data. Protected is a useful level of access control for important aspects to a class that must be passed on without allowing it to be accessed. The syntax is the same as that of public. specifically,protected:
private:
Private is the highest level of data-hiding. Not only are the functions and variables marked private not accessible by code outside the specific object in which that data appears, but private variables and functions are not inherited (in the sense that the derived class cannot directly access these variables or functions). The level of data protection afforded by protected is generally more flexible than that of the private level. On the other hand, if you do not wish derived classes to access a method, declaring it private is sensible.Best Wishes,
"...SUBHANALLAHI WABI HAMDIHI...SUBHANALLAH IL AZEEM..."
Muhammd Ishfaq
MCS 2nd Semester (PakPattan)
Muhammd Ishfaq
MCS 2nd Semester (PakPattan)
--
Remember Me In Your Prayers
Best regard's
Muhammad Afaaq
MBA 4th (Final Semester) Finance Group
Afaaq_Tariq@yahoo.com
Islamabad
For latest assignments solved quizzes files gdb solve n unsolved past papers Come join us in http://vugoogle.com
http://www.alliswell.com.pk/
http://groups.google.com/group/vustudymania
0346-5329264
If u like me than raise your hand with me
If not then raise ur standard
That's about me … !
--
Earning program links, Cell number, Websites/Groups Links, Adult contents, Spamming, Immoral & Rudish talk are strictly prohibited and banned in group.
Follow these detailed Group Rules, otherwise you will be banned at any time.
https://docs.google.com/document/d/1YJxA8x3_U7C1lRc0EXfLrJpco4A1XkB1vDxOTqOd3Jg/edit?hl=en&authkey=CNDy9tkJ
Group Email Address:
Attock-VU-Group@Googlegroups.Com
Join group by sending a blank email from University ID at:
Attock-VU-Group+Subscribe@Googlegroups.Com
Attention......!!!!!!!!!!!
Our Google Group has a problem, mostly members are facing in posting mails, If any one has this problem then forward that sending failed email at this address:
Manager.atk.grp@gmail.com
We will unsubscribe all bouncing IDs from group to resolve this issue.
Sorry for inconvenience, we have no another option for resolving this issue.
Members are requested to empty their inbox to avoid from this issue.
No comments:
Post a Comment