0% found this document useful (0 votes)
9 views22 pages

CS-Object Oriented Programming ++with C: Lecture 2: Introduction

Uploaded by

essa6alsadg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views22 pages

CS-Object Oriented Programming ++with C: Lecture 2: Introduction

Uploaded by

essa6alsadg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CS- Object Oriented Programming

++with C
1
Lecture 2: Introduction
2 Contents of course

What is Object Oriented Programming?


Objects and class
Constructor & Destructor
Operator Overloading
Inheritance
Polymorphism and virtual Functions
Abstract class
Friend Function and Friend Class
?What is Object Oriented Programming
3
▪ Object-oriented programming (OOP): is a computer
programming model that organizes software design around data, or
objects.
▪ OOP: is a programming paradigm based on the concept of objects.
▪ programming paradigm: is a style of programming ,a way of
thinking about software construction.
Programing paradigm does not refer to a specific language, but rather
it refers to the way to build a program or a methodology to apply.
▪ Objects: is a thing(Tangible-Intangible).
4
Why Do We Need Object-Oriented Programming?

Object-oriented programming was developed because limitations were


discovered in earlier approaches to programming such as procedural
approach .

Procedural Approach:

Also known as inline programming takes a top-down approach. it is


about writing a list of instructions to tell the computer what to do
step by step .it relies on procedural or routine.
When programs become larger, a single list of instruction becomes
unwieldy.
A procedural program is divided into functions.
Object Oriented Programming
5

Problems with Procedural Approach o difficult to conceptualize.


o Unrestricted Access o difficult to modify.
Object Oriented Programming
6

Problems with Procedural Approach


o Procedural code is difficult to relate with real-world objects, this may
make it difficult to design.

Real-world Programming
Attributes Data
- eye color (for people) They have a certain specific values.
- horsepower (for cars)
Behavior Functions
A Behavior is something a real-world you call a function to do something
object does in response to some (display the inventory, for
stimulus. If you apply the brakes in a example) and it does it.
car, it will generally stop.
Object Oriented Programming
7
Object-Oriented Approach

o The fundamental idea behind object-oriented languages is


to combine into a single unit both data and the functions
that operate on that data. Such a unit is called an object.
o A C++ program typically consists of a number of objects,
which communicate with each other by calling one
another’s member functions
Object Oriented Programming
8
What kinds of things become objects in object-oriented programs?
Objects in college management program

College Environment

Student

Teache
Course
r

Section Office
Object Oriented Programming
9
What kinds of things become objects in object-oriented programs?
Objects in super market program

Super Market Environment

Product

Cashier Customer

Cart Bagger
Object Oriented Programming
10

Objects is comprised of ?
Objects: comprised of associated DATA and OPERATION (methods)
which manipulate that data.

Object

Data

()Operations
? Objects is comprised of
11

Product
Data
Product_name-1
Product_code-2
Price-3
Producer-4

()Operations
()Modify price-1
()Get Product Name-2
()Get Product Price-3
? Objects is comprised of
12

Car
Data
Model-1
Fuel_capacity-2
No_of_doors-3
Color-4
Shape-5

()Operations
() Change Color-1
()Get Car Info-2
()..……-3
13 Classes And Objects

What is Class ? Why we need it?


Class: is a description of a number of similar objects.
⁻ A class is code that describes a particular type of object. It
specifies the data that an object can hold (the object's fields),
and the actions that an object can perform (the object's
methods).
⁻ You can think of a class as a code "blueprint“ that can be used
to create a particular type of object .
Class Student
14
Data: Student1
1. Student_name
2. Student_id Data:
3. Birth_date 1. Student_name
4. adress 2. Student_id Student2
5. Study_level 3. Birth_date
Email .6 4. adress Data:
Operations() 5. Study_level 1. Student_name
1. Change_study_level() Email .6 2. Student_id
2. Get_student_name() Operations() 3. Birth_date
3. Get_student_address() 1. Change_study_level() 4. adress
2. Get_student_name() 5. Study_level
()Print_student_info .4
3. Get_student_address() Email .6
()Print_student_info .4 Operations()
1. Change_study_level()
2. Get_student_name()
3. Get_student_address()
()Print_student_info .4
15 Classes And Objects

- When the program is running, it can use the class to create as many
objects of a specific type as needed.
- Each object that is created from a class is called instance of the
class.
.How to create a class ,step-by-step
16
class MyClass // The class

{ public: // Access specifier


Class MyClass int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
Data: public:
1. myNum void setdata(int d) //member function to set data
2. myString {myNum = d; }

Operations()
void readdata() //member function to read data
1. setdata()
2. readdata() { cin>> myString ;}
3. showdata()
void showdata() //member function to display data
{ cout << “My number is “ << myNum << endl;
cout << “My string is “ << myString << endl; } };
17 Classes And Objects
Class MyClass
int main()
Data: {
1. myNum
Obj1 MyClass Obj1; // Create an object of class MyClass
2. myString
Data: // Access attributes and set values
Operations()1. myNum Obj1.myNum = 15;
2. myString Obj1.myString = "Some text";
1. setdata()
2. readdata()
Operations() // Print attribute values
3. showdata() cout << Obj1.myNum << "\n";
1. setdata()
cout << Obj1.myString;
2. readdata()
return 0;
3. showdata() }
Definition Or
18 specifier
Classes And Objects
class MyClass // The class
int main()
{ private: // Access specifier
int myNum; // Attribute (int variable) { // Create an object of class MyClass
string myString; // Attribute (string variable)
MyClass Obj1,Obj2;
public:
Access specifier
void setdata(int d) //member function to set data Creating or instantiating
//call member function to set data
{myNum = d; } Obj1.setdata(1066);
void readdata() //member function to read data Obj1.readdata();
Obj2.setdata(1000);
{ cin>> myString ;}
Obj2.readdata();
void showdata() //member function to display data //call member function to display data
{ cout << “My number is “ << myNum << endl;
Obj1.showdata();
cout << “My string is “ << myString << endl; } }; Obj2.showdata();
return 0; Class member
} access operator
19 Access Modifier (Private and Public data)
Access modifier (or access specifier)is a c++ keyword that indicates
.how a data or operation can be accessed
▪ Public Data
hiding
Public data or functions are accessible from outside the class.
- when the public access modifier is applied to a class member, the
member can be accessed by code inside the class or outside.
▪ Private:
Private data or functions can only be accessed from within the class.
-when the private access modifier is applied to a class member, the
member cannot be accessed by code outside the class.The member can
be accessed only by methods that are members of the same class.
20 Data Hiding
•An object hides its internal, private fields from code that is outside the
class that the object is an instance of.
•Only the class's methods may directly access and make changes to the
object’s internal data.
•Code outside the class must use the class's public methods to operate
on an object's private fields.
Data hiding is important because classes are typically used as
components in large software systems, involving a team of
programmers.
Simple Example to create object from class car
21
A Car object will have the following fields:

Car
class Car
{ Data:
private: 1. Name
string name; 2. Color
string color; 3. Model
int model;
public: Operations()
void setdata(string n , string c, int m) //member function to set data 1. setdata()
2. showdata()
{ name = n;
color = c;
model = m; }
void showdata() //member function to display data
{ cout << "The name of car is " << name << endl;
cout << "The color of car is " << color << endl;
cout << "The model of car is " << model << endl;}};
Simple Example to create object from class car
22
A Car object will have the following fields:

()int main
}
Car c1; // Create an object of class Car
c1.setdata("Toyota","Black",2022); //call member function to set data
c1.showdata(); //call member function to display data
;return 0
}

()int main
}
Car c1,c2; // Create two object of class Car
c1.setdata("Toyota","Black",2022); //call member function to set data
c1.showdata(); //call member function to display data
c2.setdata(“Kia",“blue",2021);
c2.showdata();
;return 0
{

You might also like