Saturday, May 7, 2011

[discussion_vu] CS304_Example_inheritance



---------- Forwarded message ----------
From: ..::ISHFAQ::.. <mc100402092@vu.edu.pk>
Date: Sat, May 7, 2011 at 3:53 PM
Subject: CS304_Example_inheritance
To: , afaaq <afaaqtariq233@gmail.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)





--

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 … !


--
You received this message because you are subscribed to the Google Groups "Virtual University of Pakistan" group.
To post to this group, send email to discussion_vu@googlegroups.com.
To unsubscribe from this group, send email to discussion_vu+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/discussion_vu?hl=en.

No comments:

Post a Comment

PAID CONTENT