You are on page 1of 35

Object Oriented Programming

Tirgul 2
Classes
Constructor + Destructor

ADT ?
H :
.

C :
, .

.
ADT- stack C

#ifndef STACK_H
#define STACK_H

Stack.h

/* possible return values */


typedef enum {Fail, Success} Result ;
/* ADT of Stack of characters */
typedef struct Stack_t* Stack ;
/* stack functions */
Stack create(int max_size);
void destroy (Stack s) ;
Result push(Stack s, int elm) ;
Result pop(Stack s) ;
Result top(Stack s, int* elm) ;
int count(Stack s);
#endif

C struct-

C++-
C++- .Classes
:
.
().

.data members
( methods)
.member functions
.functions

:
H-
!!
struct Stack
{
int* array ;
int stack_top ;
int maxCapacity ;

Stack.h

/* stack methods */
Result create(int max_size);
void destroy () ;

Result push(int elm) ;

Result pop() ;
Result top(int* elm) ; !
int count();

Data members

methods

};
void function1 (int a);
int function2 ();

functions

struct Stack
{

int* array ;
int stack_top ;
int maxCapacity ;

/* stack functions */
Result create(int max_size);
void destroy () ;
Result push(int elm) ;
Result pop() ;
Result top(int* elm) ;
int count();
};


private
.private
public .

C- .
C++- ,
.

struct Stack
{
private:
int* array ;
int stack_top ;
int maxCapacity ;
public:
/* stack functions */
Result create(int max_size);
void destroy () ;
Result push(int elm) ;
Result pop() ;
Result top(int* elm) ;
int count();
};

Class
C++- " class
, .struct
?
,struct- .public
,class- .private

public / private .

Class

class
struct Stack
{
private:
int* array ;
:
int stack_top ;
.struct class "
int maxCapacity ;
public:
/* stack functions */
Result create(int max_size);
void destroy () ;
Result push(int elm) ;
Result pop() ;
Result top(int& elm) ;
int count();
};

class Stack
{
int* array ;
int stack_top ;
int maxCapacity ;
public:
/* stack functions */
Result create(int max_size);
void destroy () ;
Result push(int elm) ;
Result pop() ;
Result top(int& elm) ;
int count();
};

Stack.cpp

Result Stack::create(int max_size)


{
if (max_size <=0)
return Fail;
stack_top = 0;
maxCapacity = max_size ;
array = new int[max_size];
return Sucsess;
}

.stack

.stack

:


.

Stack.cpp

class Stack
{
; int* array
; int stack_top
; int maxCapacity
public:
/* stuck functions */
;)Result create(int max_size
; )( void destroy
; )Result push(int elm
; )(Result pop
; )Result top(int& elm
;)(int count
;}



.
.OOP
.
)Result Stack::create(int max_size

{
.
)if (max_size <=0
.
;return Fail

;stack_top = 0

; maxCapacity = max_size
!
;]array = new int[max_size
;return Sucsess
!
}

stack-

C-

.
C++-

.

#include Stack.h
)(int main
{
;Stack stack
;int i
;)stack.create(100
;)stack.push(1
;)(stack.pop
;)stack.top(i
}

, ,
() .this
this .
( const"
)
, >this-

this -> stack_top = 0; stack_top = 0; :

this

.
returnBiggest :
;) Stack* Stack::returnBiggest (Stack& s

.

this
Stack* Stack::returnBiggest(Stack& s)
{
if (stack_top < s.stack_top)
return &s;
else
return ?this;
}
:this :

Constructors & destructors


create- ?destroy-
" , C++-
.
( )constructor
.
( ,)destructor
~.

Stack.h

class
struct Stack
{
private:
int* array ;
int top ;
int maxCapacity ;
public:
/* stuck functions */
Stack(int
max_size);
Result create(int
max_size);
~Stack();
void destroy () ;
Result push(int elm) ;
Result pop() ;
Result top(int* elm) ;
int count();
};

constructor

destructor

Constructor & destructor


//constructor
Stack::Stack (int max_size )
{
if (max_size <=0)
exit(1);
top = 0;
maxCapacity = max_size ;
array = new int [max_size];
}
//destructor
Stack::~Stack()
{
delete[] array;
}

Default Constructor
constructor- ,
..
:default constructor
constructor
constructor- .
function overloading
, -constructor .
, destructor- .

Default Constructor
class Stack
{
private:
int* array ;
int top ;
int maxCapacity ;
public:
/* stuck functions */
Stack();
Stack(int max_size);
max_size=10);
~Stack();
Result push(int elm) ;
Result pop() ;
Result top(int* elm) ;
int count();
};

//default constructor
Stack::Stack ()
{
top = 0;
maxCapacity = 10;
array = new int [10];
}
Default constructor

Constructor & destructor


constructor/ ?destructor
.

( . )

?
.
, , ,
, ,
.

Employee

.
:
.
.

:
.
.

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#define MAX_NAME_SIZE 40
#define MAX_SALARY_ALLOWED 50000

class Employee
{
private:
char name[MAX_NAME_SIZE];
int salary;
public:
Employee();
Employee (const char* name, int salary);
~Employee();
void printAttributes();
void construct (const char *name , int salary);
void setName (const char* name);
void setSalary (int salary);

};
#endif

Default constructor
constructor

Destructor

#include "Employee.h"
#include <iostream>
using namespace std;

Employee.cpp

Employee::Employee()
{
cout << default constructor called" << endl;
strcpy(name, Not set yet);
salary = 0;
}

Default Constructor

Employee::Employee (const char *emp_name, int emp_salary)


{
strcpy(name, emp_name);
Constructor
salary = emp_salary;
cout << "constructor called" << endl;
printAttributes();
}
Employee::~Employee()
Destructor
{
cout << "destructor called :"<< name<<","<<salary << endl;
}

Employee.cpp
void Employee::construct (const char* emp_name , int emp_salary)
{
strncpy(name, emp_name, MAX_NAME_SIZE);
salary = emp_salary;
}
void Employee::printAttributes()
{
cout << "Employee Name : " << name << endl;
cout << "Employee Salary : " << salary << endl << endl;
}

int main()
{
Employee emp1;
Employee emp2)Livnat Cohen, 12000);
cout << "emp1 after creation:" << endl;
emp1.printAttributes();
emp1.construct)Sharoni Feldman", 50000);
cout << "emp1 after Construct:" << endl;
emp1.printAttributes();
cout << "emp2 after creation:" << endl;
emp2.printAttributes();
return 0;
}

Default constructor called


constructor called
Employee Name : Livnat Cohen
Employee Salary : 12000
emp1 after creation:
Employee Name : Not set yet
Employee Salary : 0
emp1 after Construct:
Employee Name : Sharoni Feldman
Employee Salary : 50000
emp2 after creation:
Employee Name : Livnat Cohen
Employee Salary : 12000
destructor called :Livnat Cohen, 12000
destructor called :Sharoni Feldman,
50000

Set & Get Functions


.

( )

!
:2
setSalary .

?
setName .

Set & Get Functions


?
?
..


get .

Set & Get Functions


void Employee::setName (const char* emp_name)
{
strcpy (name, emp_name);
}
void Employee::setSalary (int emp_salary)
{
salary = emp_salary;
}
const char* Employee::getName ()
{
return name;
}
int Employee::getSalary ()
{
return salary;
}

void function)(
}
static Employee emp("Jenny K", 9000);
//do nothing
{
Employee emp0;
int main)(
}
emp0.SetName("Tali Fr");
emp0.SetSalary(5000);
function)(;
Employee emp2("Hofit BE", 200);
Employee* p_emp;
function)(;
p_emp = new Employee("Michal Y",
8000);
delete p_emp;
return 0;
{

default constructor called


constructor called
Employee Name : Jenny K
Employee Salary : 9000
constructor called
Employee Name : Hofit BE
Employee Salary : 200
constructor called
Employee Name : Michal Y
Employee Salary : 8000

destructor called :Michal Y,8000


destructor called :Hofit BE,200
destructor called :Jenny K,9000
destructor called :Tali Fr,5000


.
.
, , ,'.
LinkedList


class LinkedList
int length;
Node* list_head;

class Node
Data* data;
Node* next;

#include <iostream>
using namespace std;

LinkedList.h

...

class Linklist
{
private:
class Node
{
public:
Data* data;
Node* next;
}
Node* list_head;
public:
linklist();
~linklist();
Result insert (Data data);
Result deleteElement (Data data);
Result deleteNum (int num);
...
};

//insert a new element with the data data


// delete the element with the data data
//delete the numth element


?
class Employee

class LinkedList

char* name;

int length;
Node* list_head;

int salary;

class Node
Employee* emp;
Node* next;

You might also like