c++ classes
TRANSCRIPT
C++ Classes
Qazi Haseeb Yousaf BCS-IV (Morning B)
C.No 22
In The Real World Life
Real world objects have two major things
1. State/Attributes (what it is) and
2. Behavior/Properties/Actions (what it does) To simulate real world objects in
software system, we can use
Data Abstraction
A Real World Object
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.1. Combining Attributes and Behaviors.
ENCAPSULATION
2. 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:
1. Class head:1. – Keyword class followed by the class name.
2. Class body:1. – A collection of data members2. – A collection of member functions(methods)3. – 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
1. Data Members are kept Private.
2. 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 memoryIn 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 initializedint 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!