c++ classes

28
C++ Classes Qazi Haseeb Yousaf BCS-IV (Morning B) C.No 22

Upload: imhammadali

Post on 20-Aug-2015

1.886 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C++ classes

C++ Classes

Qazi Haseeb Yousaf BCS-IV (Morning B)

C.No 22

Page 2: C++ classes

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

Page 3: C++ classes

A Real World Object

Page 4: C++ classes

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

Page 5: C++ classes

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

Page 6: C++ classes

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.

Page 7: C++ classes

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.

Page 8: C++ classes

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.

Page 9: C++ classes

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)

Page 10: C++ classes

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

Page 11: C++ classes

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.

Page 12: C++ classes

A simple class

class Cat { unsigned int itsAge; unsigned int itsWeight;

Meow(); };

class exforsys { int x,y;

void sum() {

……… ……… }

};

Page 13: C++ classes

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.

Page 14: C++ classes

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.

Page 15: C++ classes

Class Example

class Cat { private:unsigned int itsAge; unsigned int itsWeight;

public:Meow();

};

class exforsys { public: void sum() {

……… ……… }

private: int x,y; };

Page 16: C++ classes

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

Page 17: C++ classes

• Object = (private) data + (public) operations

Page 18: C++ classes

C++ Objects

Page 19: C++ classes

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;

Page 20: C++ classes

Creating an object (Syntax)

class exforsys { public: void sum() {

……… ……… }

private: int x,y; }obj;

void main(){class exforsys obj1;exforsys obj2;}

Page 21: C++ classes

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()}

Page 22: C++ classes

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.

Page 23: C++ classes

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};

Page 24: C++ classes

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; } ….};

Page 25: C++ classes

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.

Page 26: C++ classes

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(); }

Page 27: C++ classes

Example (output)

Example of one argument Constructor

e1=50

Example of default Constructor

e2=0

Example of Copy Constructor

e3=50

Page 28: C++ classes

That’s All

THANKS!