• No results found

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

N/A
N/A
Protected

Academic year: 2021

Share "Chapter 4 OOPS WITH C++ Sahaj Computer Solutions"

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 4 Chapter 4

(2)

Session Objectives

Classes and Objects Class Declaration Class Members Data Constructors Data Constructors Destructors Member Functions

Class Member Visibility Private, Public,Protected

(3)

Session objectives

Default Constructor

Constructor with arguments

Constructor with default arguments

Dynamic constructor

Dynamic constructor

Copy Constructor

Overloaded Constructor

Objects as Function arguments

Member functions defined outside the class. Objects as arguments

(4)

Session Objectives

Returning objects from functions Class array as class member data Array of objects

String as class member.

(5)

Accessing Class Members

To access the members of the class we make use of the

DOT(.) Operator. Syntax: Object-name.function-name(actual arguments) Object-name.function-name(actual arguments) Example: x.getData(100,75.5);

(6)

Defining Member Functions

Member functions can be defined in two places.

Outside the Class definition Inside the class definition

Outside the Class Definition Outside the Class Definition

Member functions that are declared inside the class

have to e defined separately outside the class.

Their definition is much like normal functions. They should have a function header and a body.

(7)

Outside the Class Definition

An important difference between a member function and a

normal function is that a member function incorporates a membership ‘identity label’ in the header.

This label tells the compiler which class the function This label tells the compiler which class the function

belongs to.

The general form of a member-function definition is :

return-type class-name:: function-name(argument declaration) {

Function-Body }

(8)

Outside the Class Definition

The membership label class-name:: tells the compiler

that the function-name belongs to the class-name .

That is the scope of the function is restricted to the

class name specified in the header line. class name specified in the header line.

The symbol :: is called the scope resolution operator.

Example:

void sum::getdata(int a,int b) {

sum=a+b; }

(9)

Defining Member Functions

The member functions have some special

characteristics that are often used in the program development:

Several different classes can use the same function Several different classes can use the same function

name. The membership label will resolve their scope.

Member functions can access the private data of the

class. A non-member function cannot do so.

A member function can call another member function

(10)

Inside the class definition

Another method of

declaring a member

function is to replace the function definition by the actual definition.

Example

class sum{ int a,b; public:

the actual definition. public:

void getdata(int x,int y) {

a=x; b=y }

(11)

Making Outside function Inline

A member function outside the class definition can still be

made inline by using the qualifier inline.

Example:

class item{ :::::::::::::::::: :::::::::::::::::: public:

void getdata(int a,int b); };

inline void item::getdata(int a,int b) {

:::::::::::::::::::::: }

(12)

Nesting Member Functions

As discussed earlier a member function of a class can

be called by an object of that class using the dot operator.

However there is an exception to this. However there is an exception to this.

A member function can be called by using its name

inside another member function of that same class.

This is known as nesting of member functions

(13)

Arrays within a class

Arrays can be used as member variables in a class For example: class arraydemo{ int a[20]; int a[20]; public: void getval(){ for(int i=0;i<n;i++) cin>>a[i]; } }; Program: CH4PG8.cpp

(14)

Memory Allocation for Objects

Member variable 1 Member variable 2 Member function1 Class Member variable 1 Member variable 2 Member variable 1 Member variable 2 Member variable 1 Member variable 2 Member function1 Member function2

(15)

Static Data Members

Whenever a object is created a new copy of data

variables is created for that object.

This means that each objects works on its own copy of

data variables. data variables.

C++ uses another type of variable declaration called as

static.

A data member of class can be qualified as static The static member variable has certain special

(16)

Static Data Members

These are:

It is initialized to zero when the first object of its class is

created. No other initialization is permitted.

Only one copy of that member is created for the entire Only one copy of that member is created for the entire

class and is shared by all the objects of that class, no matter how many objects are created.

It is visible only within the class, but its lifetime is the

(17)

Static Data Members

PROGRAM:CH4PG9.CPP

Note that the type and scope of each static member

int item :: count ;

Note that the type and scope of each static member

variable must be defined outside the class definition.

This is necessary because the static data members are

stored separately rather than as a part of an object.

Since they are associated with class itself rather than

(18)

Static Data Members

number number number

Object A Object B Object C

count

(19)

Static Member Functions

Like static member variables, C++ has static member

functions.

A member function that is declared as static has

following properties: following properties:

A static function can have access to only other static

members (functions or variables) declared in the same class.

A static member function can be called using the class

name (instead of its object)

Syntax: class-name::function-name; Program: Ch4Pg10.cpp

(20)

Array of Objects

Arrays can be of any data type including class and

struct.

Such variables are called array of objects. Syntax; Syntax; class-name object-name[size]; Example: employee manager[20]; Program :CH4PG11.cpp

(21)

Objects as function arguments

Like any other data type, an object may be used as a

function argument.

This can be done in two ways:

A copy of the entire object is passed to the function. A copy of the entire object is passed to the function.

Only address of the object is transferred to the function.

(22)

Friend Functions

It is possible to grant a non-member function access to

the private members of a class by using a friend.

A friend function has access to all private and

protected members of the class for which it is a friend. protected members of the class for which it is a friend.

To declare a friend function, include its prototype

within the class, preceding it with the keyword friend.

Syntax:

friend type fn-name(arg-list){ //function body

(23)

Friend Function

In this example, the sum() function is not a member of

myclass.

However, it still has full access to its private members. Also, notice that sum() is called without the use of the Also, notice that sum() is called without the use of the

dot operator.

Because it is not a member function, it does not need

to be (indeed,it may not be) qualified with an object's name.

(24)

Friend Function

A friend function possesses certain special

characteristics:

It is not in the scope of the class to which it has been

declared as friend.

Since it is not in the scope of the class, it cannot be

called using the object of that class.

It can be invoked like normal function without the help

of any object.

It can be declared in public or private part of the class

without affecting its meaning.

(25)

A function friendly to two classes

Program:CH4PG14.cpp

The function max() has arguments from both XYZ and

ABC.

When the function max() is declared as a friend When the function max() is declared as a friend

function in XYZ for the first time, the compiler will not acknowledge the presence of ABC unless its name is declared in the beginning as

class ABC;

(26)

Friend Classes

It is possible for one class to be a friend of another

class.

When this is the case, the friend class and all of its

member functions have access to the private members member functions have access to the private members defined within the other class.

For example,

class Y {

friend class X; };

(27)

Class

It is possible to define short functions completely

within a class declaration.

When a function is defined inside a class declaration,

it is automatically made into an inline function (if it is automatically made into an inline function (if possible).

It is not necessary (but not an error) to precede its

declaration with the inline keyword.

(28)

Returning Objects

A Function can not only receive objects as arguments

but also can return them .

The following program demonstrates this concept.

Program: CH4PG17.cpp

(29)

Structures and Classes Are Related

A class is syntactically similar to a struct. But the

relationship between a class and a struct is closer than you may at first think.

In C++, the role of the structure was expanded,

In C++, the role of the structure was expanded,

making it an alternative way to specify a class.

In fact, the only difference between a class and a struct

is that by default all members are public in a struct and private in a class.

(30)

Unions and Classes Are Related

Like a structure, a union may also be used to define a

class.

In C++, unions may contain both member functions

and variables. and variables.

They may also include constructor and destructor

functions.

A union in C++ retains all of its C-like features, the

most important being that all data elements share the same location in memory.

(31)

Constructors

A constructor is a special member function whose task is

to initilize the object of its class.

The constructor is invoked whenever the object of the class

is created. Syntax: Example: Syntax: class class-name{ public: class-name() //constructor { } }; Example: class integer{ int x,y; public: integer() { x=0; y=0; } };

(32)

Constructors

Characteristics of constructors;

They should be declared in public section.

They have no return type not even void and therefore

they do not return values. they do not return values.

They cannot be inherited , though derived class

constructors can call base class constructor.

Constructors cannot be virtual.

(33)

Types of constructors

There are three types of constructors in C++:

Default Constructors

Parameterized Constructors Copy constructors

Copy constructors

Default Constructors:

A default constructor is a constructor which does not

(34)

Parameterized Constructors

A constructor which accepts arguments is called as

parameterized constructor.

Syntax:

class-name :: class-name(argument-list) class-name :: class-name(argument-list) {

//body of the constructor } Example: class integer{ int x,y; public: integer(int a, int b) { x=a; y=b }

(35)

Parameterized Constructors

An parameterized constructor can be called using two

ways

By calling the constructor explicitly By calling the constructor implicitly By calling the constructor implicitly

Examples:

integer i=integer(10,20); //explicit call integer i(10,20); //implicit call

(36)

Constructor Overloading

A class can have one or more than one constructors

defined in a class with different number of arguments or different type of arguments.

This is called constructor overloading. This is called constructor overloading.

The compiler recognizes the constructor depending

upon the number of arguments or type of arguments.

(37)

Copy Constructor

A Constructor which accepts an copy of its own class as

an argument is called an copy constructor.

Syntax:

class-name:: class-name(class-name &obj) class-name:: class-name(class-name &obj)

{

//constructor body }

A copy constructor is used to declare and initialize an

(38)

Dynamic Constructor

The constructor can also be used to allocate memory

while creating objects.

This ensures the system to allocate the right amount of

memory for each object when the objects are not of memory for each object when the objects are not of the same size, thus resulting in the saving of memory.

Allocation of memory to objects at the time of their

(39)

Arguments

It is possible to define constructors with default

arguments.

For example:

Complex( float real, float img=0); Complex( float real, float img=0);

The argument img=0. Then the statement

Complex(5.0);

Assigns 5.0 to real and 0 to img (by default) variables.

Complex(2.0,3.0);

(40)

Near Gomatesh School, Hindwadi Belgaum-11, Phone no: 4200864,2481703 Get Certified in •Microsoft Office 2010 •Tally 9.2 •.NET 2008 •J2EE •CORE JAVA

Around 1800 students

developed software projects

and scored the top scores in

exams.

One stop shop for

•CORE JAVA •C PROGRAMMING •OOPS WITH C++ •JOOMLA •WORDPRESS •PHP AND MYSQL •SQLSERVER

•UNIX AND LINUX

•ANDRIOD APPS

One stop shop for

Software Projects

Website Development

Technology Training I.T Research

References

Related documents

constructors performed a defacto type conversion from the argument's type to the constructor's class type. The variables length and name are data members of the class string. Once

• We defined an operator* function that accepts a Vector3D, but we can make it accept other types too. • We can overload the

When the dataset size is larger, 40 blocks, the proposed scheme shows lower average access time than those of the random selection scheme and the SLF scheme.. Still, the SLF

class, only the public and protected members of base class can be accessed by the member functions of derived class. This means no private member of the base class

It shows that the debates on the sharing economy are characterised by value disputes, uncertain facts, high stakes and the need of urgent decision; despite the

 private class members can be accessed only by the class member functions (and friend functions), not by client code... Aggregate

16.10 When Constructors and Destructors Are Called 16.11 Using Data Members and Member Functions 16.12 A Subtle Trap: Returning a Reference to a private Data Member.. 16.13

6.13 When Constructors and Destructors Are Called 6.14 Using Data Members and Member Functions 6.15 A Subtle Trap: Returning a Reference to a.. Private