Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

CSE 1205: Object Oriented Programming

Classes & Objects


Md Mehrab Hossain Opi
Class 2
• As we discussed before class is a blueprint of objects.
Class
• It encapsulates data and function into a single unit.
Data

data1
data2
data3

Functions

func1()
func2()
func3()

CSE 1205: Object Oriented Programming June 5, 2024


Structures 3
• Class is an extension of the idea of structure used in C.
• Let’s recap the basics of structure before diving into class.

CSE 1205: Object Oriented Programming June 5, 2024


Structure 4
• A structure is a collection of simple variables.
• A user defined data-type that allows you to group together related data items of different
types under a single name.
• Let’s look at a simple structure.

struct SimpleStructure{
int data1;
float data2;
};

CSE 1205: Object Oriented Programming June 5, 2024


Structure Syntax 5
• The basic syntax of the structure

struct struct_name {
// Member variables (fields)
data_type_1 member_1;
data_type_2 member_2;
// ...
data_type_n member_n;
};

CSE 1205: Object Oriented Programming June 5, 2024


Using Structure 6
int main(){

struct SimpleStructure s1; // Declaring a variable.


s1.data1 = 0; // Accessing member variable

• To declare a variable of user-defined data type we must use struct keyword.


• The member variables are accessed using the dot operator.

CSE 1205: Object Oriented Programming June 5, 2024


Structure and Class 7
• A structure is a collection of data, while a class is a collection of both data and
functions.
• C structure does not permit data hiding.
• The standard C does not allow the struct data type to be treated like built-in types.

CSE 1205: Object Oriented Programming June 5, 2024


A Simple Class 8

Let’s look at the definition of a simple class in C++.

class SimpleClass{

private:
int some_data;
Let’s understand how a class
public: works now.
void set_data(int d){
some_data = d;
}
void show_data(){
cout<<"Data is: "<<some_data<<endl;
}

};

CSE 1205: Object Oriented Programming June 5, 2024


Defining a Class 9
• The class definition starts with the keyword class.
• Followed by the class name.
• Do you remember the naming conventions in C?

• The body of a class is delimited by braces and terminated by a semicolon.

class class_name{
// Body of class
};

CSE 1205: Object Oriented Programming June 5, 2024


Body of a Class 10
• The body of a class contains both data and functions.
• You can define (or declare) functions as you used to define them globally.

class class_name{
int data_1;
float data_2;

void print(){
cout<<"Hello";
}
int sum(int a,int b){
return a+b;
}
};

CSE 1205: Object Oriented Programming June 5, 2024


Body of a Class 11
• The data items within a class are called data members
• Or sometimes member data.

• Member functions are functions that are included within a class.


• Sometimes also called methods.

CSE 1205: Object Oriented Programming June 5, 2024


Syntax of a Class 12
• Let’s look at the class we declared before again.

class SimpleClass{

private:
What does the word int some_data;
private/public mean?
public:
void set_data(int d){
some_data = d;
}
void show_data(){
cout<<"Data is: "<<some_data<<endl;
}

};

CSE 1205: Object Oriented Programming June 5, 2024


private and public 13
• Data and Functions are usually grouped into two sections
• private and public.

• The keywords are known as visibility labels.


• Notice the keywords are followed by a colon.
• The class members declared as private can be accessed only from within the class.
• public members can be accessed from outside the class too.

CSE 1205: Object Oriented Programming June 5, 2024


Data Hiding 14
• A key feature of OOP is data hiding.
• Data is concealed within a class so that it cannot be accessed mistakenly by functions
outside the class.
• The primary mechanism is to put data in a class and make it private.

CSE 1205: Object Oriented Programming June 5, 2024


Hidden from Whom? 15
• Don’t confuse data hiding with the security techniques.
• For security you might use username & password.
• The password is meant to keep unauthorized users from accessing the data.
• Data hiding means something else.

CSE 1205: Object Oriented Programming June 5, 2024


Hidden from Whom? 16
• Data hiding means hiding data from parts of the program that don’t need to access it.
• It is designed to protect programmers from honest mistakes.
• Programmers who want to can figure out a way to access private data, but they will find it
hard to do so by accident.

CSE 1205: Object Oriented Programming June 5, 2024


Using the Class 17
• Now that the class is defined, let’s see how main makes use of it.
• We’ll see how objects are defined and how their member functions are accessed.

CSE 1205: Object Oriented Programming June 5, 2024


Defining Objects 18
• Remember that the declaration of class does not define any objects.
• Only defines what the object will contain.

• We can create an object by using the class name.

class SimpleClass{
int main(){
private: // Declaring an Object.
int some_data; SimpleClass simpleObject;
public: }
void set_data(int d){
some_data = d;
}
void show_data(){
cout<<"Data is: "<<some_data<<endl;
}

};

CSE 1205: Object Oriented Programming June 5, 2024


Defining Objects 19
• Space is set aside for objects in memory when they are defined.
• Defining objects in this way means creating them.
• This is also called instantiating them.
• An object is an instance of a class.
• Objects are sometimes called instance variables.

CSE 1205: Object Oriented Programming June 5, 2024


Defining Objects 20
• Objects can also be created when a class is defined by placing their names immediately
after the closing brace.
class SimpleClass{
private:
int some_data;
public:
void set_data(int d){
some_data = d;
}
void show_data(){
cout<<"Data is: "<<some_data<<endl;
}
} simpleObject1, simpleObject2;

CSE 1205: Object Oriented Programming June 5, 2024


Accessing Member Data 21
• The private data of a class can be accessed through the member function of that class.
• The main cannot contain statements that access private members.

int main(){
SimpleClass simpleObject; // Declaring an Object.
simpleObject.some_data = 101; // Produces Error
}

CSE 1205: Object Oriented Programming June 5, 2024


Calling Member Function 22
• The format for calling a member function
object_name.function_name(actual_arguments);

• For example

int main(){
SimpleClass simpleObject; // Declaring an Object.
simpleObject.set_data(10); // Calling member function.
simpleObject.show_data();
}

CSE 1205: Object Oriented Programming June 5, 2024


Messages 23
• Some object-oriented languages refer to calls to a member function as messages.
• The call simpleObject.show_data() can be thought of as sending a message to
simpleObject to show its data.
• The objects communicate with each other by sending and receiving messages.
• Hence the term “Message Passing”.

CSE 1205: Object Oriented Programming June 5, 2024


Defining a Member Function 24
• Earlier we saw in class declaration we completely defined the functions.
• But there are two different methods for specifying functions.
• Outside the class definition
• Inside the class definition

• There is a subtle difference between these two.

CSE 1205: Object Oriented Programming June 5, 2024


Outside the Class Definition 25
• In this case we just declare the functions inside class definition.
• Do you remember how to declare a function?
• A function declaration tells the compiler about the number of parameters, data types of
the parameters, and return type of function.

int sum_of_two(int a,int b);

CSE 1205: Object Oriented Programming June 5, 2024


Outside the Class Definition 26
• Let’s look at a example
class SimpleClass{

private:
int some_data;

public:
void set_data(int d);
void show_data();
};

• We declared two functions that doesn’t have a definition.


• And we will define it outside the class definition.

CSE 1205: Object Oriented Programming June 5, 2024


Outside the Class Definition 27
• Now how do we define those functions?
• We can’t just write
void set_data(int d){
some_data = d;
}

• The compiler has no way to relate this function with our class definition.

CSE 1205: Object Oriented Programming June 5, 2024


Outside the Class Definition 28
• To relate the function we need to attach the class name somewhere.
• The correct syntax

void SimpleClass::set_data(int d){


some_data = d;
}

• We got some unfamiliar syntax now.


• The function name is preceded by the class name
• And a new symbol – the double colon (::)

• This symbol is called the scope resolution operator.


• It is a way of specifying what class something is associated with.

CSE 1205: Object Oriented Programming June 5, 2024


CSE 1205: Object Oriented Programming June 5, 2024 29

Thank You.

You might also like