it's about object-oriented, Slides of Computer science

it's about object-oriented, it will help you to know the basics of OOP

Typology: Slides

2022/2023

Uploaded on 02/28/2023

emad-selkhi
emad-selkhi 🇵🇸

5 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Review:
Two Programming Paradigms
Structural (Procedural) Object-Oriented
PROGRAM PROGRAM
FUNCTION
FUNCTION
FUNCTION
OBJECT
Operations
Data
OBJECT
Operations
Data
OBJECT
Operations
Data
Function calls Messages passing
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download it's about object-oriented and more Slides Computer science in PDF only on Docsity!

Review:

Two Programming Paradigms

Structural (Procedural) Object-Oriented

PROGRAM PROGRAM

FUNCTION

FUNCTION

FUNCTION

OBJECT

Operations

Data

OBJECT

Operations

Data

OBJECT

Operations

Data

Function calls Messages passing

Object-Oriented Design Goals:

  • (^) Robustness
    • (^) We want software to be robust , that is, capable of

handling unexpected inputs that are not explicitly defined

for its application.

  • (^) Adaptability (also called evolvability)
    • (^) Related to this concept is portability , which is the ability

of software to run with minimal change on different

hardware and operating system platforms.

  • (^) Reusability
    • (^) That is, the same code should be usable as a component

of different systems in various applications.

Review: Object-Oriented

Programming Language Features

1. Data abstraction

2. Inheritance of properties

3. Dynamic binding of operations to objects

Review: C++ Data Types Review: C++ Data Types

structured

array struct union class

address

pointer reference

Simple buit-in

integral enum

char short int long bool

Short x = 1;

floating

float double long double

union employee

double salary;

int id;

Char c;

employee e;

e.Id = 500; e.salary = 4000;

4000

Object-Oriented Programming--

Introduction to Classes

• Class Definition

• Class Examples

• Objects

class Rectangle

{

private:

int width;

int length;

public:

void set(int w, int l);

int area();

};

Classes & Objects

Rectangle r1;

Rectangle r2;

Rectangle r3;

int a;

Define a Class Type

class class_name

permission_label:

member ;

permission_label:

member ;

class Rectangle

{

private:

int width;

int length;

public:

void set(int w, int l);

int area();

};

Body

Header

class Rectangle

private:

int width;

int length;

public:

static int count;

void set(int w, int l);

int area();

int Rectangle::count = 0;

Static Data Member

Rectangle r1;

Rectangle r2;

Rectangle r3;

Cout<< r1.count;

Cout<<Rectangle::count;

width

length

width

length

width

length

r

r

r

count

Class Definition – Member

Functions(methods)

  • (^) Used to
    • (^) access the values of the data members ( accessor )
    • (^) change the value of a private instance variable in a

class ( mutator methods or setters or set or modifier

methods).

  • (^) perform operations on the data members (implementor)
  • (^) Are declared inside the class body, in the same way as

declaring a function

  • (^) Their definition can be placed inside the class body, or

outside the class body

  • (^) Can access both public and private members of the class
  • (^) Can be referred to using dot or arrow member access

operator

i

class String{

private :

char * s;

public:

int length() const { int i=0 ; for(; s[i];i++); return i; }

char* copy(string & dest, const string & src) {

char* temp = new char[-----];

for(; i< src.length() ; i++)

return temp;

}};

Void main(){

String s,t;

char* p = s.copy(s,t);

temp

p

17

  • (^) static member function
    • (^) A static member can be referred to without mentioning an object

( function call outside of the class, but need to be with the name of it’s

class.

class Rectangle{

private:

double width, length;

static int count;

public:

Rectangle(double w=0, double l=0){

width = w; length= I;

count++;

}

inline double area() const { return width*length; }

static int getCount(){

return count; }

};

Class Definition – static member functions

Const Member Function

class Time

{

private :

int hrs, mins, secs ;

public :

void Write ( ) const;

void Write ( ) ;

} ;

void Time :: Write( ) const

{

cout <<hrs << “:” << mins << “:” << secs << endl;

}

function declaration

function definition

20

  • (^) Information hiding
    • (^) To prevent the internal representation from direct

access from outside the class

  • (^) Access Specifiers
    • (^) public
      • (^) may be accessible from anywhere within a program
    • (^) private
      • (^) may be accessed only by the member functions, and

friends of this class, not open for nonmember functions

  • (^) protected
    • (^) acts as public for derived classes (virtual)
    • (^) behaves as private for the rest of the program
  • (^) Difference between classes and structs in

C++

the default access specifier is private in classes

the default access specifier is public in structs

Class Definition - Access Control