Professional Documents
Culture Documents
Tirgul 2
Classes
Constructor + Destructor
ADT ?
H :
.
C :
, .
.
ADT- stack C
#ifndef STACK_H
#define STACK_H
Stack.h
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
.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
.
returnBiggest :
;) Stack* Stack::returnBiggest (Stack& s
.
this
Stack* Stack::returnBiggest(Stack& s)
{
if (stack_top < s.stack_top)
return &s;
else
return ?this;
}
:this :
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
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
?
.
, , ,
, ,
.
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.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;
}
!
:2
setSalary .
?
setName .
get .
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;
{
.
.
, , ,'.
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);
...
};
?
class Employee
class LinkedList
char* name;
int length;
Node* list_head;
int salary;
class Node
Employee* emp;
Node* next;