0% found this document useful (0 votes)
4 views16 pages

Lecture 3 - Programming - 2 - Class

The document outlines the objectives and principles of Object-Oriented Programming (OOP) as part of a Programming 2 course. It explains key concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with access specifiers and member functions. Additionally, it provides examples of class definitions and member specifications in C++ programming.

Uploaded by

mahmouddelsisii
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)
4 views16 pages

Lecture 3 - Programming - 2 - Class

The document outlines the objectives and principles of Object-Oriented Programming (OOP) as part of a Programming 2 course. It explains key concepts such as classes, objects, encapsulation, inheritance, and polymorphism, along with access specifiers and member functions. Additionally, it provides examples of class definitions and member specifications in C++ programming.

Uploaded by

mahmouddelsisii
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

PROGRAMMING 2

(CS 103)
3

LECTURE 3 CLASSES AND OBJECTS


SPRING (2026)
COURSE LEARNING OBJECTIVES
1. Design and implement multi-class solutions to solve complex problems using Object-Oriented

Programming (OOP).

2. Apply the principles of encapsulation, inheritance, and polymorphism to create modular and reusable

software.

3. Manage dynamic memory and object lifecycles using pointers to optimize software performance and

scalability.

4. Collaborate in a team environment to develop software that meets defined functional and non-

functional requirements.

2
CLASSES AND OBJECTS
 A Class is like a blueprint and objects are like houses built from the blueprint.

5 3
PROCEDURAL PROGRAMING AND OBJECT-
ORIENTED PROGRAMING
Procedural programming focuses on the process/actions
that occur in a program.
– Tells the computer what to do step by step

Object-Oriented programming ( OOP ) is based on the data


and the functions that operate on it. Objects are
instances that represent the data and its functions.
– All computations are carried through OBJECTS

– OBJECT:An element that knows how to act and how to interact


with other elements
4
OBJECT-ORIENTED PROGRAMMING
• Everything is an object.

• Objects can
• act/interact
• have their own memory

• Every object is an instance of a class


• The class holds the shared
behavior for its instances.
• Every object has a unique identity, state, and behavior.

5
OBJECT-ORIENTED PROGRAMMING
TERMINOLOGY
▪ Attributes / data fields / properties:
– Member properties of a class

▪ Methods / behaviors / functions:


– Member functions of a class

6
MORE ON OBJECTS
Data hiding:
– restricting access to certain members of an object

Public interface:
– members that are available outside of the object

Allows the object to


– provide access to some data and functions Data Data
without sharing its internal details and design
– provide some protection from data corruption Func1 Func2

7
HOW TO DECLARE A CLASS?

8
CLASS DEFINITION:
 Class: a data type that contains member variables
and functions used to perform operations on their class ClassName
{
variables (objects). public:

MemberSpecification_1;
 Object: an instance/variable of the class. MemberSpecification_2;
...
MemberSpecification_n;
 Class is declared globally outside of main function.
private:

 Objects are created within the main function by MemberSpecification_n+1;


MemberSpecification_n+2;
specifying the class name followed by object name. ...
};

Each MemberSpecification_i is either a member variable declaration or a member function declaration.


(Additional public and private sections are permitted.)

9
MEMBER SPECIFICATION
▪ Member Variable (Data Member)
A variable declared in the class.
Example:
int age;
string name;
double salary;
➢ These represent the data (attributes) of the class.
▪ Member Function (Method)
A function declared in the class.
Example:
void setAge(int a);
int getAge();
void display();
➢ These represent the behavior (actions) of the class.
10
CLASS EXAMPLE
class Circle Optional Circle
{ default −radius: double
private: value
+getArea(): double
double radius = 1; Member data field
public:
double getArea(); Member function

}; // Must place a semicolon here


Creating the Object ClassName ObjectName;

Example:
Circle circle_1;
11
EX: CLASS DEFINITION
#include <iostream>
using namespace std;

class Rectangle { Defines a new class named Rectangle.


private:
double length, width;
•These are data members (attributes).
public: •private → cannot be accessed directly from main().

double getArea() {
•This is a member function (method).
return length * width;
} •It calculates and returns the area of the rectangle.
};

int main( ) { This creates an object named rectangle1 from the class
Rectangle rectangle1; // creating object Rectangle.
return 0; Object = Instance of a class.
}
12
ACCESS SPECIFIERS/MODIFIERS

 Private:

Private members can be accessed only from within the class.

 Public:

Public members can be accessed from outside the class.

 Protected:

Protected members can be accessed from within the class and its derived classes.

- By default, if not stated, Class members are private, Unlike struct.

- Private data members can be accessed by functions that are wrapped inside the class.

13
EX: CLASS
#include <iostream>
using namespace std;

class Rectangle {
private:
double length, width;

public:
void setValues(double l, double w) {
length = l;
width = w; }

double getArea() {
return length * width; }
};
int main() {
Rectangle rectangle1;
[Link](5, 3);
cout << "Area = " << [Link]();
return 0;
}
14
MEMBER VARIABLE/FUNCTION DEFINITION
 You can define member variable/function #include <iostream>
using namespace std;
class Rectangle {
of a class inside it normally, or outside of the
private:
class using scope resolution operator ( :: ) double length, width;
public:
void setArea(double l, double w);
Example is perfect for explaining: double getArea();
• Function declaration inside the class };
• Function definition outside the class void Rectangle::setArea(double l, double w) {
length = l;
• Scope resolution operator ::
width = w;}
• Dot operator . to call object functions double Rectangle::getArea() {
return length * width;}
int main() {
Rectangle rectangle1; // creating object
[Link](2.5, 4);// dot operator
cout << "Area: "
<< [Link]() << endl;
return 0;}

15

You might also like