0% found this document useful (0 votes)
21 views

Lab 3

Uploaded by

mayadanasr996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lab 3

Uploaded by

mayadanasr996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lab 3

Classes & Objects 1

1
Table of Contents

• What is a class?
• What is an Object?
• Class Vs. Struct
• Methods Vs. Functions
• Example on class
• Access modifiers
• Private
• Protected
• Public

2
 What is a class?

• Image from the image that you need to make application for lion animals
• After 6 months the client told you he added a new animal (elephant) in his zoo and need you
to add it to the application
• If you didn’t use classes you have to build another side application for only elephant that
will be same very much to the lions code with little change
• This will make a problem you duplicated so much of code that will slow the application
• BUT if you used classes from the beginning you just need to make object from animal
class and update the information of the elephant, this will reduce the code and will avoid
duplication
• So after 2 years the application contained 6 types of animals which have similarities, classes
helped you to add each new animal without write code from scratch to each one of them.
• This is the power of classes
• Organize the code
• Reduce modification time
• Easy to read and check error on it

3
 What is a class?

• Is the most important invention in the history of programming


• All higher order languages are OOP languages such as python, java, C, C++, C#, etc..
• Class is simulation for the real life
• Any object in life has two things (property and behavior) for example:-
• Animal
• Property: name, age, type, color, breed, etc..
• Begavior: walk, eat, play, sleep, run, etc..

4
 What is a class?

The name of the class, it is the name of the


Class keyword that defines the new data type data type such as int, float, bool, etc…
called Student like int, float…

class Student{ Properties/variables of class

Public:
Public keyword makes the class data as public
access from any where int id;
Member/method of the class
string name;
void getStudentData();
};

Class should be ended with semicolon

5
 What is an object?
• Object
• It is also called an instance
• The object takes a copy from the class properties and members to RAM
• So if you have two objects from the same class (stu1, stu2) each one has its own copy in RAM, when you change stu1 data, it will not affect on stu2

Make object from this class called stu1

int main() {
Student stu1 = Student(); Object can access the class’s data members
and properties
We use dot (.) with objects stu1.name = "ahmed";
stu1.id = 10;
cout<<stu1.name<<" "<<stu1.id<<endl; // ahmed 10
return 0;
}

make instance from class without new keyword (called object)

6
 What is an object?
• Pointer
• So object has another name which is instance
• If we used * with the object it will be called pointer and use arrow (->) to access the object’s data
• if we don’t use * the object will be called also object or instance and we use dot (.) to access the object’s data

Make pointer from class called *stu1

int main() {

We use arrow (->) with pointers Student *stu1 = new Student(); pointer can access the class’s data members
stu1->name = "ahmed"; and properties

stu1->id = 10;
cout<<stu1->name<<" "<<stu1->id<<endl; // ahmed 10
return 0;
}

make instance from class with new keyword (called pointer)

7
 Class Vs. Struct

• Class and struct have the same purpose and can contain properties and members but class most popular used

Class Struct
Is reference type: send object to function parameters (change in Is value type: send object to function parameters (change in it
it will also change the original data) will not change the original data)

Use struct to make new data type like block contains all block
Use classes to make operations on data using the methods
info such as Student(name, age, address, ….)

Used in small, medium and large applications Used in small, medium applications

Used if needed in the application to specific piece of code that


Mostly used than struct makes it increase the performance or make code easy to read
and understand

Contains inheritance concept

class can contain prosperities and methods

8
 Class Vs. Struct

This code show that they both can do the same work but class mostly used than struct
Struct mostly used when create a complex data type that contains many data i.e (Employee struct contains [name, job, salary, etc..])

class Employee{
Use EmployeeData struct as data type struct EmployeeData {
EmployeeData empData;
// get employee information from DB string name, address;
void getEmpInfo(){ } double salary, extraHour;
// calculate the total salary employee
double calculateTotalSalary() { int empId;
return empData.salary + empData.extraHour; };
}
};

Class can used empData to store its information in one block rather than Use struct to make new data type called EmployeeData to store all the
used them separately as properties in it, this will make the code more employee information in one block to used it any where
readable

9
 Methods Vs. Functions
The only difference between methods and functions is that the function inside the class called method

methods Functions

class Employee{ void getEmpInfo(){


EmployeeData empData; // get employee information from DB
void getEmpInfo(){ }
// get employee information from DB
}
double calculateTotalSalary() {
double calculateTotalSalary() {
return empData.salary +
return empData.salary + empData.extraHour;
empData.extraHour;
}
}
};

10
 Example on class
• The following code uses bank employees data to
• Display it on screen
• Calculate their total salary

Build bank employee class contains its info and operations that need to do on him
• Calculate its total salary and return it
• Display his information on the screen to the manager Use the employee class by making new object and deal with its data and methods

class BankEmployee{ int main() {


public: BankEmployee emp = BankEmployee();
string name, address, jobTitle; getline(cin, emp.name);
double salary, extraHour; getline(cin, emp.address);
int id, age; getline(cin, emp.jobTitle);
cin>>emp.age>> emp.salary >> emp.extraHour;
double claculateTotalSalary() {
return salary + extraHour; // get its total salary
} double totalSalary = emp.claculateTotalSalary();
void displayEmpData(){ // display all its info for the manager
cout<< name << ", " << address << endl; emp.displayEmpData();
cout<< jobTitle << ", " << age << endl; return 0;
} }
}; 11
TRY IT YOURSELF

• Create class called Student which contains the following:


• Properties
• Name: String
• Age: int
• Address: String
• Id: String
• Grade: int
• Members
• getData() // get the student data from the user
• displayData() // print all the student data
• Create object from the class in main function and call the members
• NOTE: don’t forget public keyword

12
TRY IT YOURSELF
solution
class Student{ int main() {
string name, address, id; Student st = Student();
int age, grade; st.getData();
public: st.displayData();
void getData(){ return 0;
getline(cin, name); }
getline(cin, address);
getline(cin, id);
cin>> age >> grade;
}

void displayData() {
cout<<"Student Info is: \n";
cout<<"id: " << id<<endl;
cout<<"name: " << name<<endl;
cout<< "address: " << address<<endl;
cout<< "age: " << age<<endl;
cout<< "grade: " << grade<<endl;
}
};

13
 Access Modifiers

There are three types of access modifiers make the class’s data available to other users as we need
• Private (default)
• Only the methods inside the class can use it
• Even the object that I make in main function cannot see these private data
• Protect
• Members inside the class can see the data
• Object which is made inside main function cannot see the protected data
• Also the direct child/derived class can see the protected data
• Public
• Class’s data is available to anyone anywhere

14
 Access Modifiers
• Private
• Only members inside the class can use private data (figure 1)
• Object cannot see the private data (figure 2)
• It is the default keyword so no need to write it explicitly

1 2

15
 Access Modifiers
• protected
• Members inside class can see protected data
• Direct derived class can see protect data (discuss in inheritance lab) (figure 1)
• Object cannot see protected data (figure 2)

1 2

Class Dog inherits Animal’s members since they are


protected but cannot see properties since they are
private by default 16
 Access Modifiers
• public
• Public data can be seen anywhere anytime

Object can see class public data

17
THANK YOU

18

You might also like