0
C++ Classes Qazi Haseeb Yousaf BCS-IV ( Morning B) C.No 22 Presented By
In The Real World Life <ul><li>Real world objects have two major things </li></ul><ul><ul><li>State/Attributes (what it is...
A Real World Object ATTRIBUTES BEHAVIORS
What is Data Abstraction? <ul><li>A data abstraction is a simplified view of an object that  </li></ul><ul><ul><li>include...
For Object-Orientation  <ul><li>By seeing objects of real world and  </li></ul><ul><li>Data Abstraction  </li></ul><ul><li...
Encapsulation <ul><li>It is a mechanism that associates the code  (Behavior)  and the data  (Attribute)  it manipulates in...
Information Hiding <ul><li>Data hiding means to secure data or sometimes specific behaviors from direct access. </li></ul>...
C++ Class <ul><li>Abstract Data Type  (ADT) is the key to Object-Oriented programming. An ADT is a set of data together wi...
C++ Class <ul><li>A  class definition  has two parts: </li></ul><ul><li>Class head : </li></ul><ul><ul><li>–  Keyword  cla...
C++ Class (Syntax) <ul><li>class class_name  </li></ul><ul><li>{  </li></ul><ul><li>permission_label_1: member1;  </li></u...
Members of A Class <ul><li>Member variables  ,  also known as data members , are the variables in your class. Member varia...
A simple class <ul><li>class Cat  </li></ul><ul><li>{  </li></ul><ul><li>unsigned int itsAge;  </li></ul><ul><li>unsigned ...
Access Specifiers <ul><li>A  private  member  within a class denotes that only members of the same class have accessibilit...
Access Specifiers (contd…) <ul><li>Normally in accordance to the rules of Data Abstraction/information hiding  </li></ul><...
Class Example <ul><li>class Cat  </li></ul><ul><li>{  </li></ul><ul><li>private: </li></ul><ul><li>unsigned int itsAge;  <...
OBJECT <ul><li>What is an object? </li></ul><ul><li>In software design: an entity in system model </li></ul><ul><li>In sou...
<ul><li>Object = (private) data + (public) operations </li></ul>
C++ Objects
Creating Objects <ul><li>//create a variable x of type integer,  </li></ul><ul><li>// x is not initialized </li></ul><ul><...
Creating an object (Syntax) <ul><li>class exforsys  </li></ul><ul><li>{  </li></ul><ul><li>public: void sum()  </li></ul><...
Accessing Class Members <ul><li>An object can access its public members through a dot operator. </li></ul><ul><ul><ul><li>...
Constructor <ul><li>Constructor is a member function of a class, which is called automatically whenever an object is insta...
General Syntax of Constructor <ul><li>Constructor is a special member function that takes the same name as the class name....
Default Constructor: <ul><li>This constructor has no arguments in it. Default Constructor is also called as  no argument c...
Copy constructor: <ul><li>This constructor takes one argument. Also called one argument constructor. The main use of copy ...
Example (coding) <ul><li>#include <iostream.h>  </li></ul><ul><li>class Exforsys()  </li></ul><ul><li>{  </li></ul><ul><li...
Example (output) <ul><li>Example of one argument Constructor  </li></ul><ul><li>e1=50 </li></ul><ul><li>Example of default...
That’s All THANKS!
Upcoming SlideShare
Loading in...5
×

C++ classes

1,826

Published on

C++ Classes

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,826
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
165
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "C++ classes"

  1. 1. C++ Classes Qazi Haseeb Yousaf BCS-IV ( Morning B) C.No 22 Presented By
  2. 2. In The Real World Life <ul><li>Real world objects have two major things </li></ul><ul><ul><li>State/Attributes (what it is) and </li></ul></ul><ul><ul><li>Behavior/Properties/Actions (what it does) </li></ul></ul><ul><li>To simulate real world objects in software system, we can use </li></ul><ul><li>Data Abstraction </li></ul>
  3. 3. A Real World Object ATTRIBUTES BEHAVIORS
  4. 4. What is Data Abstraction? <ul><li>A data abstraction is a simplified view of an object that </li></ul><ul><ul><li>includes only features one is interested in. </li></ul></ul><ul><ul><li>while hides away the unnecessary details. </li></ul></ul><ul><li>In programming languages, a data abstraction becomes an abstract data type (ADT) or a user-defined type. </li></ul><ul><li>In OOP, it is implemented as a class </li></ul>
  5. 5. For Object-Orientation <ul><li>By seeing objects of real world and </li></ul><ul><li>Data Abstraction </li></ul><ul><li>we have to cop with two Major TASKS. </li></ul><ul><li>Combining Attributes and Behaviors. </li></ul><ul><ul><li>ENCAPSULATION </li></ul></ul><ul><li>Hiding Unnecessary details. </li></ul><ul><ul><li>INFORMATION HIDING </li></ul></ul>
  6. 6. Encapsulation <ul><li>It is a mechanism that associates the code (Behavior) and the data (Attribute) it manipulates into a single unit to keeps them safe from external interference and misuse. </li></ul><ul><li>In C++, we use CLASS to Encapsulate Attribute and Behavior . </li></ul>
  7. 7. Information Hiding <ul><li>Data hiding means to secure data or sometimes specific behaviors from direct access. </li></ul><ul><li>Why we do so? </li></ul><ul><li>C++ provides Access Specifier for this Purpose. </li></ul>
  8. 8. C++ Class <ul><li>Abstract Data Type (ADT) is the key to Object-Oriented programming. An ADT is a set of data together with the operations on the data . </li></ul><ul><li>A class is often used to describe an ADT in C++. </li></ul><ul><li>A class is also called a User-Defined Type . </li></ul><ul><li>A class is a logical method to organize data and functions in the same structure. </li></ul>
  9. 9. C++ Class <ul><li>A class definition has two parts: </li></ul><ul><li>Class head : </li></ul><ul><ul><li>– Keyword class followed by the class name. </li></ul></ul><ul><li>Class body : </li></ul><ul><ul><li>– A collection of data members </li></ul></ul><ul><ul><li>– A collection of member functions(methods) </li></ul></ul><ul><ul><li>– Levels of access control. (Access Specifier) </li></ul></ul>
  10. 10. C++ Class (Syntax) <ul><li>class class_name </li></ul><ul><li>{ </li></ul><ul><li>permission_label_1: member1; </li></ul><ul><li>permission_label_2: member2; ... </li></ul><ul><li>}; </li></ul><ul><li>Member include member variables and member functions. </li></ul><ul><li>Values for permission_label: private, protected, public </li></ul>Don’t Forget this Semi-colon
  11. 11. Members of A Class <ul><li>Member variables , also known as data members , are the variables in your class. Member variables are part of your class, just like the wheels and engine are part of your car. </li></ul><ul><li>Member functions , also known as methods , are the functions in your class. Member functions are as much a part of your class as the member variables. They determine what the objects of your class can do. </li></ul>
  12. 12. A simple class <ul><li>class Cat </li></ul><ul><li>{ </li></ul><ul><li>unsigned int itsAge; </li></ul><ul><li>unsigned int itsWeight; </li></ul><ul><li>Meow(); </li></ul><ul><li>}; </li></ul><ul><li>class exforsys </li></ul><ul><li>{ </li></ul><ul><li>int x,y; </li></ul><ul><li>void sum() </li></ul><ul><li> { </li></ul><ul><li> ……… ……… </li></ul><ul><li>} </li></ul><ul><li> }; </li></ul>
  13. 13. Access Specifiers <ul><li>A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class. </li></ul><ul><li>Public members are accessible from outside the class. (only through class object). </li></ul><ul><li>A protected access specifier is a stage between private and public access. If member functions defined in a class are protected , they cannot be accessed from outside the class but can be accessed from the derived class. </li></ul>
  14. 14. Access Specifiers (contd…) <ul><li>Normally in accordance to the rules of Data Abstraction/information hiding </li></ul><ul><ul><li>Data Members are kept Private. </li></ul></ul><ul><ul><li>Methods are kept Public. </li></ul></ul><ul><li>In this way you can save data from misuse. </li></ul><ul><li>**Example: Telephone diary+Mechanic. </li></ul>
  15. 15. Class Example <ul><li>class Cat </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>unsigned int itsAge; </li></ul><ul><li>unsigned int itsWeight; </li></ul><ul><li>public: </li></ul><ul><li>Meow(); </li></ul><ul><li>}; </li></ul><ul><li>class exforsys </li></ul><ul><li>{ </li></ul><ul><li>public: void sum() </li></ul><ul><li> { </li></ul><ul><li> ……… ……… </li></ul><ul><li>} </li></ul><ul><li> private: int x,y; </li></ul><ul><li> }; </li></ul>
  16. 16. OBJECT <ul><li>What is an object? </li></ul><ul><li>In software design: an entity in system model </li></ul><ul><li>In source file: a typed variable( an instance of a class ) </li></ul><ul><li>In object file: an allocation of memory </li></ul><ul><li>In run-time memory: a portion of memory </li></ul>
  17. 17. <ul><li>Object = (private) data + (public) operations </li></ul>
  18. 18. C++ Objects
  19. 19. Creating Objects <ul><li>//create a variable x of type integer, </li></ul><ul><li>// x is not initialized </li></ul><ul><li>int x; </li></ul><ul><li>//create three objects of class BankAccount </li></ul><ul><li>// initialized ? </li></ul><ul><li>BankAccount account1, account2, account3; </li></ul>
  20. 20. Creating an object (Syntax) <ul><li>class exforsys </li></ul><ul><li>{ </li></ul><ul><li>public: void sum() </li></ul><ul><li> { </li></ul><ul><li> ……… ……… </li></ul><ul><li>} </li></ul><ul><li> private: int x,y; </li></ul><ul><li> }obj; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>class exforsys obj1; </li></ul><ul><li>exforsys obj2; </li></ul><ul><li>} </li></ul>
  21. 21. Accessing Class Members <ul><li>An object can access its public members through a dot operator. </li></ul><ul><ul><ul><li>E.g. obj.function_name(); </li></ul></ul></ul><ul><li>class exforsys </li></ul><ul><li>{ </li></ul><ul><li>private: int y; </li></ul><ul><li>public:int x; </li></ul><ul><li>void sum() { ……… ……… } </li></ul><ul><li>void accessy(){………………} </li></ul><ul><li> }; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>exforsys obj; </li></ul><ul><li>obj.sum(); </li></ul><ul><li>obj.x=9; </li></ul><ul><li>obj.y=8; // ERROR!’y’ is a privately accessed member </li></ul><ul><li>obj.accessy() </li></ul><ul><li>} </li></ul>
  22. 22. Constructor <ul><li>Constructor is a member function of a class, which is called automatically whenever an object is instantiated. </li></ul><ul><li>What is the use of Constructor? </li></ul><ul><ul><li>The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor. </li></ul></ul>
  23. 23. General Syntax of Constructor <ul><li>Constructor is a special member function that takes the same name as the class name. </li></ul><ul><li>The syntax generally is as given below: </li></ul><ul><li>class_name(arguments) {initialization of data members}; </li></ul>
  24. 24. Default Constructor: <ul><li>This constructor has no arguments in it. Default Constructor is also called as no argument constructor . </li></ul><ul><li>For example: </li></ul><ul><li>class exforsys </li></ul><ul><li>{ private: int a,b; </li></ul><ul><li> public: exforsys(){ a=0; b=0; } </li></ul><ul><li>… .}; </li></ul>
  25. 25. Copy constructor: <ul><li>This constructor takes one argument. Also called one argument constructor. The main use of copy constructor is to initialize the objects while in creation, also used to copy an object. The copy constructor allows the programmer to create a new object from an existing one by initialization. </li></ul>
  26. 26. Example (coding) <ul><li>#include <iostream.h> </li></ul><ul><li>class Exforsys() </li></ul><ul><li>{ </li></ul><ul><li>private: int a; </li></ul><ul><li>public: </li></ul><ul><li>Exforsys() {a=0; cout<<” nExample of default Constructor”;} </li></ul><ul><li>Exforsys(int w) { a=w; cout<<” nExample of one argument Constructor”;} </li></ul><ul><li>Exforsys(Exforsys& e) { a=e.a; cout<<” nExample of Copy Constructor”; } </li></ul><ul><li>void result() { cout<< a; } </li></ul><ul><li>}; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><li>Exforsys e1(50); </li></ul><ul><li>cout<< “ne1=”;e1.result(); </li></ul><ul><li>Exforsys e2(); </li></ul><ul><li>cout<< “ne2=”;e2.result(); </li></ul><ul><li>Exforsys e3(e1); </li></ul><ul><li>cout<< “ne3=”;e3.result(); </li></ul><ul><li>} </li></ul>
  27. 27. Example (output) <ul><li>Example of one argument Constructor </li></ul><ul><li>e1=50 </li></ul><ul><li>Example of default Constructor </li></ul><ul><li>e2=0 </li></ul><ul><li>Example of Copy Constructor </li></ul><ul><li>e3=50 </li></ul>
  28. 28. That’s All THANKS!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×