SlideShare a Scribd company logo
C++ Classes Qazi Haseeb Yousaf BCS-IV ( Morning B) C.No 22 Presented By
In The Real World Life Real world objects have two major things State/Attributes (what it is) and Behavior/Properties/Actions (what it does)  To simulate real world objects in software system, we can use Data Abstraction
A Real World Object ATTRIBUTES BEHAVIORS
What is Data Abstraction? A data abstraction is a simplified view of an object that  includes only features one is interested in.  while hides away the unnecessary details.  In programming languages, a data abstraction becomes an abstract data type (ADT) or a user-defined type.  In OOP, it is implemented as a class
For Object-Orientation  By seeing objects of real world and  Data Abstraction  we have to cop with two Major  TASKS. Combining Attributes and Behaviors. ENCAPSULATION Hiding Unnecessary details. INFORMATION HIDING
Encapsulation 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.  In C++, we use CLASS to Encapsulate Attribute and Behavior .
Information Hiding Data hiding means to secure data or sometimes specific behaviors from direct access. Why we do so? C++ provides Access Specifier for this Purpose.
C++ Class 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 . A  class  is often used to describe an ADT in C++. A class is also called a  User-Defined Type .  A class is a logical method to organize data and functions in the same structure.
C++ Class A  class definition  has two parts: Class head : –  Keyword  class  followed by the class name. Class body : –  A collection of data members –  A collection of member functions(methods) –  Levels of access control. (Access Specifier)
C++ Class (Syntax) class class_name  {  permission_label_1: member1;  permission_label_2: member2; ...  };   Member include member variables and member functions. Values for permission_label: private, protected, public Don’t Forget this Semi-colon
Members of A Class 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.  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.
A simple class class Cat  {  unsigned int itsAge;  unsigned int itsWeight;  Meow(); };  class exforsys  {  int x,y;  void sum()    {  ……… ………  }   };
Access Specifiers 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.  Public  members  are accessible from outside the class. (only through class object).  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.
Access Specifiers (contd…) Normally in accordance to the rules of Data Abstraction/information hiding  Data Members are kept Private. Methods are kept Public. In this way you can save data from misuse. **Example: Telephone diary+Mechanic.
Class Example class Cat  {  private: unsigned int itsAge;  unsigned int itsWeight;  public: Meow(); };  class exforsys  {  public: void sum()    {  ……… ………  }   private: int x,y;   };
OBJECT What is an object? In software design: an entity in system model In source file: a typed variable( an instance of a class ) In object file: an allocation of memory In run-time memory: a portion of memory
Object = (private) data + (public) operations
C++ Objects
Creating Objects //create a variable x of type integer,  // x is not initialized int x; //create three objects of class BankAccount  // initialized ? BankAccount account1, account2, account3;
Creating an object (Syntax) class exforsys  {  public: void sum()    {  ……… ………  }   private: int x,y;   }obj; void main() { class exforsys obj1; exforsys obj2; }
Accessing Class Members An object can access its public members through a dot operator. E.g. obj.function_name(); class exforsys  {  private: int y; public:int x;  void sum() { ……… ……… } void accessy(){………………}   };  void main() { exforsys obj; obj.sum(); obj.x=9; obj.y=8; // ERROR!’y’ is a privately accessed member obj.accessy() }
Constructor Constructor is a member function of a class, which is called automatically whenever an object is instantiated. What is the use of Constructor?  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.
General Syntax of Constructor Constructor is a special member function that takes the same name as the class name. The syntax generally is as given below:  class_name(arguments) {initialization of data members};
Default Constructor: This constructor has no arguments in it. Default Constructor is also called as  no argument constructor .  For example:  class exforsys  { private: int a,b;    public: exforsys(){ a=0; b=0; }  … .};
Copy constructor: 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.
Example (coding) #include <iostream.h>  class Exforsys()  {  private: int a;  public:  Exforsys() {a=0; cout<<” \nExample of default Constructor”;}  Exforsys(int w) { a=w; cout<<” \nExample of one argument Constructor”;} Exforsys(Exforsys& e) { a=e.a; cout<<” \nExample of Copy Constructor”; }  void result() { cout<< a; }  }; void main()  {  Exforsys e1(50); cout<< “\ne1=”;e1.result();   Exforsys e2();  cout<< “\ne2=”;e2.result();   Exforsys e3(e1); cout<< “\ne3=”;e3.result();   }
Example (output) Example of one argument Constructor  e1=50 Example of default Constructor  e2=0 Example of Copy Constructor  e3=50
That’s All THANKS!

More Related Content

PPTX
Assembly language (addition and subtraction)
PPTX
Protection Domain and Access Matrix Model -Operating System
PPTX
Lecture 02: Preliminaries of Data structure
PPTX
Addressing sequencing
PPT
C++ Arrays
PDF
Java threads
PPT
Unit 4 designing classes
PPT
Complexity of Algorithm
Assembly language (addition and subtraction)
Protection Domain and Access Matrix Model -Operating System
Lecture 02: Preliminaries of Data structure
Addressing sequencing
C++ Arrays
Java threads
Unit 4 designing classes
Complexity of Algorithm

What's hot (20)

PDF
Assistant Programmer, Bangladesh Bank
PPTX
Introduction to Array ppt
PPTX
PPTX
Data Types - Premetive and Non Premetive
PPTX
Principal of objected oriented programming
PPTX
Addressing Modes.pptx
PPTX
Input-Buffering
PPTX
Static and dynamic scoping
PDF
Compiler unit 1
PPTX
Modes of transfer
PPTX
Intermediate code generator
PPT
Os Swapping, Paging, Segmentation and Virtual Memory
PPTX
Graph coloring using backtracking
PPTX
Union in c language
PPT
Time complexity
PPT
Data Structures- Part5 recursion
PPTX
Register transfer language
PPTX
Lec 2 algorithms efficiency complexity
PPTX
Register transfer and micro operation
PDF
Arrays in Java
Assistant Programmer, Bangladesh Bank
Introduction to Array ppt
Data Types - Premetive and Non Premetive
Principal of objected oriented programming
Addressing Modes.pptx
Input-Buffering
Static and dynamic scoping
Compiler unit 1
Modes of transfer
Intermediate code generator
Os Swapping, Paging, Segmentation and Virtual Memory
Graph coloring using backtracking
Union in c language
Time complexity
Data Structures- Part5 recursion
Register transfer language
Lec 2 algorithms efficiency complexity
Register transfer and micro operation
Arrays in Java
Ad

Viewers also liked (20)

PPTX
Oop c++class(final).ppt
PPTX
class and objects
PPT
C++ classes tutorials
PPT
Oops ppt
PPT
Basic concepts of object oriented programming
PPTX
Classes And Objects
PPT
Class and object in C++
PPT
Object Oriented Programming Concepts
PPT
Object oriented programming (oop) cs304 power point slides lecture 01
PPTX
Introduction to Object Oriented Programming
PPT
Object-oriented concepts
PPTX
C++ classes
PPT
Object-Oriented Programming Concepts
PPTX
C++ Pointers
PPT
Object and class
PPT
C++ classes tutorials
PPTX
Stream classes in C++
PPTX
C++ language
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Oop c++class(final).ppt
class and objects
C++ classes tutorials
Oops ppt
Basic concepts of object oriented programming
Classes And Objects
Class and object in C++
Object Oriented Programming Concepts
Object oriented programming (oop) cs304 power point slides lecture 01
Introduction to Object Oriented Programming
Object-oriented concepts
C++ classes
Object-Oriented Programming Concepts
C++ Pointers
Object and class
C++ classes tutorials
Stream classes in C++
C++ language
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Ad

Similar to C++ classes (20)

PDF
oopm 2.pdf
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPTX
Object oriented programming in C++
PPTX
class c++
PPTX
OBJECT ORIENTED PROGRAMING IN C++
PDF
Introduction to C++ Class & Objects. Book Notes
PPTX
Class and object
PPTX
Concept of Object-Oriented in C++
PPTX
Introduction to Fundamental of Class.pptx
PPTX
Class and object C++.pptx
PPTX
Lecture 4. mte 407
PPTX
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
PPTX
Oop objects_classes
PDF
Chapter 7 C++ As OOP
PPTX
C++ training
PPTX
Classes and objects
PPT
Major terminologies in oop (Lect 2).ppt. Object oriented programming
PDF
Class and object
PPTX
05 Object Oriented Concept Presentation.pptx
PPT
classes & objects.ppt
oopm 2.pdf
classandobjectunit2-150824133722-lva1-app6891.ppt
Object oriented programming in C++
class c++
OBJECT ORIENTED PROGRAMING IN C++
Introduction to C++ Class & Objects. Book Notes
Class and object
Concept of Object-Oriented in C++
Introduction to Fundamental of Class.pptx
Class and object C++.pptx
Lecture 4. mte 407
ECAP444 - OBJECT ORIENTED PROGRAMMING USING C++.pptx
Oop objects_classes
Chapter 7 C++ As OOP
C++ training
Classes and objects
Major terminologies in oop (Lect 2).ppt. Object oriented programming
Class and object
05 Object Oriented Concept Presentation.pptx
classes & objects.ppt

C++ classes

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