Oop - Transperencies Ch 02

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Oop - Transperencies Ch 02 as PDF for free.

More details

  • Words: 1,749
  • Pages: 24
Chapter 02 Class and Objects

Class declaration

class class_name {

private: variables declarations; functions declarations;

public: variables declarations; functions declarations; };

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-1-

Chapter 02 Class and Objects

Class private area No Entry in private

X

Data Functions

Data Entry allowed in public

Functions public area

Data hiding in class

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-2-

Chapter 02 Class and Objects

Defining member function outside of the class return-type class-name :: function-name(arguments) { // function body } Scope resolution operator

e. g. class Bowler { int runsGiven; float overs; public: void readValue(int rg, float ov ); float economyRate( ); }; void Bowler :: readValue(int rg, float ov) { runsGiven = rg; overs = ov; } float Bowler :: economyRate( ) { return(runsGiven/overs); }

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-3-

Chapter 02 Class and Objects

Nesting of member functions #include class Finder { int num1, num2; public: void input(int m, int n); int largest(); void display(); }; void Finder::input(int m, int n) { num1 = m; num2 = n; } int Finder::largest( ) { if(num1>num2) return num1; else return num2; } void Finder::display( ) { cout<<"Largest: "<
-4-

Chapter 02 Class and Objects

Private member functions

class Reader { int val; void read(); public: void update(); void write(); }; Reader rd; // rd is object then

rd.read( ) // won’t work However the function read() can be called by the functions update( ) or by write(). Such as,

void Reader :: update() { read(); //direct call //no object used } Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-5-

Chapter 02 Class and Objects

#include class Sorter { int num[10]; public: void input( ); void sort( ); void largest( ); }; void Sorter::input( ) { for(int x=0;x<10;x++) cin>>num[x]; } void Sorter::sort( ) { for(int x=0;x<10;x++) for(int y=x;y<10;y++) if(num[x]>num[y]) { num[x] = num[x] + num[y]; num[y] = num[x] - num[y]; num[x] = num[x] - num[y]; } for(x=0;x<10;x++) cout<<"\n"<
-6-

Chapter 02 Class and Objects

Common for all objects Data members

Member functions

Object1

Object2

Memory created when functions defined

Object3

Data members

Data members

Data members

Member functions

Member functions

Member functions Memory created when objects defined

Objects in Memory

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-7-

Chapter 02 Class and Objects

Static data members class Counter { static int count; public: void get(int x); void getCount(); }; void Counter :: get(int x) { count = x; } void Counter :: getCount() { count++; cout<<"\nCount: "<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-8-

Chapter 02 Class and Objects

Object ‘a’

Object ‘b’

Object ‘c’

get( ) getCount( )

get( ) getCount( )

get( ) getCount( )

count Common for all three objects

Sharing of static data members

C haracteristics: 1. It is initialized to zero when the first object of its class is created. No other initialization is permitted. 2. Only one copy of that member is created and shared by all the objects of the class. 3. It is visible only within the class but its lifetime is the entire program.

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

-9-

Chapter 02 Class and Objects

Static member function class Test { int num; static int count; public: void set(int x); static void showCount( ); }; void Test :: set(int x) { count = x; } void Test :: showCount( ) { count++; cout<<"\nCount: "<
- 10 -

Chapter 02 Class and Objects

Array of objects class Student { char name[20]; float marks; public: void accept( ); void print( ); }; void Student :: accept( ) { cout<<"\nEnter name: "; cin>>name; cout<<"\nEnter marks: "; cin>>marks; } void Student :: print( ) { cout<<"\nName: "<
- 11 -

Chapter 02 Class and Objects

Storage of data items of an object array name

accept( )

marks

print( )

name

accept( )

marks

print( )

name

accept( )

marks

print( )

name

accept( )

marks

print( )

name

accept( )

marks

print( )

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

SYIT[0]

SYIT[1]

SYIT[2]

SYIT[3]

SYIT[4]

- 12 -

Chapter 02 Class and Objects

Object as function arguments class Square { public: int number; void input(int); int largeNum(Square, Square); }; void Square::input(int num) { number = num; } int Square::largeNum(Square s, Square t) { if(s.number > t.number) return(s.number); return(t.number); } int main( ) { Square s1,s2,s3; s1.input(45); s2.input(29); cout<<"Large:"<<s3.largeNum(s1,s2); return(0); }

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 13 -

Chapter 02 Class and Objects

Object as function arguments class Time { int hrs; int min; public: void getTime(int h, int m) { hrs = h; min = m; } void putTime( ) { cout<
- 14 -

Chapter 02 Class and Objects

hrs

6

x.hrs

15

min (x + y)

2

y.hrs

45 x.min

3 30

y.min

z.addTime(x, y)

Accessing members of object within a called function

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 15 -

Chapter 02 Class and Objects

Before studying friend functions class MyClass { int num1, num2; public: void initialize( ); int average(MyClass s); }; void MyClass::initialize( ) { num1 = 10; num2 = 20; } int average(MyClass s) { return((s.num1+s.num2)/2); } int main() { MyClass x; x.initialize(); cout<<"Average:"<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 16 -

Chapter 02 Class and Objects

Characteristics of friend function 1. It is not in the scope of the class to which it has been declared as ‘friend’. 2. Since it is not in the scope of the class, it can not be called using object of the class. 3. It can be invoked like a normal function without the help of any object. 4. Unlike member functions, it can not access the member names directly and has to use an object name and dot membership operator with each member name (e.g. obj.x). 5. It can be declared either in public or the private part of the class without affecting its meaning. 6. Usually, it has objects as arguments.

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 17 -

Chapter 02 Class and Objects

class Slave; class Master { int x; public: void initialize(int i) { x = i; } friend void findMax(Master, Slave); }; class Slave { int y; public: void initialize(int j) { y = j; } friend void findMax(Master, Slave); }; void findMax(Master m, Slave s) { if(m.x rel="nofollow"> s.y) cout<<"Max:"<<m.x; else cout<<"Max:"<<s.y; } int main( ) { Master a; Slave b; a.initialize(13); b.initialize(19); findMax(a,b); return 0; }

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 18 -

Chapter 02 Class and Objects

Function returning object class Nation { float x,y; public: void enter(float m, float n) { x = m; y = n; } void show(Nation ind) { cout<<"\nFirst:"<
Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 19 -

Chapter 02 Class and Objects

Operator Overloading

return-type classname :: operator(op-arglist) { //function body }

We can’t overload these operators: 1.

Class member selection (. and .*)

2.

Scope resolution operator (::)

3.

Size of operator (sizeof)

4.

Conditional operator( : ? )

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 20 -

Chapter 02 Class and Objects

Process of defining overloaded operators: 1.

Create a class that defines the data type that is to be used in the overloading operation.

2.

Declare the operator function in the public part of the class. It may be either a member function or a friend function.

3.

Define the operator function to implement the required operations.

Object Oriented Programming (SYIF) 2008-2009 by Kute T. B.

- 21 -

Chapter 02 Class and Objects

#include class Space { int x,y; public: void get(int a, int b) { x = a; y = b; } void show() { cout<<"\n X = "<<x; cout<<"\n Y = "<
- 22 -

Chapter 02 Class and Objects

#include class Index { int val; public: Index(){} Index(int z) { val = z; } void show() { cout<<"\n val = "<
- 23 -

Chapter 02 Class and Objects

class Overload { int n1,n2,n3; public: Overload(){} Overload(int a,int b,int c) { n1 = a; n2 = b; n3 = c; } void show() { cout<<"\n n1 = "<
- 24 -

Related Documents