0% found this document useful (0 votes)
52 views40 pages

Programming Fundamentals: Practical#10)

The document discusses object-oriented programming concepts including: 1. Classes are templates that define the structure and behavior of objects through instance variables and methods. Objects are instances of classes that can represent real-world entities. 2. Common OOP concepts include classes, objects, encapsulation, inheritance, and polymorphism. Programming languages that support these concepts are considered object-oriented. 3. Instance variables store data for each object, and methods define operations that can be performed on the object. Setter and getter methods allow external access to private instance variables.

Uploaded by

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

Programming Fundamentals: Practical#10)

The document discusses object-oriented programming concepts including: 1. Classes are templates that define the structure and behavior of objects through instance variables and methods. Objects are instances of classes that can represent real-world entities. 2. Common OOP concepts include classes, objects, encapsulation, inheritance, and polymorphism. Programming languages that support these concepts are considered object-oriented. 3. Instance variables store data for each object, and methods define operations that can be performed on the object. Setter and getter methods allow external access to private instance variables.

Uploaded by

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

Programming Fundamentals

in
C++

(PRACTICAL#10)
Object Oriented Programming

Objective: Introduction to Object Oriented Programming, to become


familiar with Objects and classes, declaring a class with instance variables and
methods and Instantiating an object of a class, and become familiar with:
instance variables and methods
access specifiers : private, public and protected
set() and get() functions.
Constructor and Destructor.
Static (class) variables
Object Oriented Programming

 What is a Programming Paradigm ?

Definition: A programming paradigm is a style


or “way” of programming. 
Some of the more common paradigms are:
 Structured — Programs have clean, goto-free, nested control structures.
 Array-based —
 Functional Programming
Object Oriented Programming

Some of the more common paradigms are:


Event-Driven — Control flow is determined by
asynchronous actions (from humans or sensors).
GUI-Based
Programming Paradigm

Imperative — Control flow is an explicit sequence of commands.

Procedural — Imperative programming with procedure calls.

Flow-Driven — Computation is specified by multiple processes


communicating over predefined channels.

Logic(Rule-based) — Programmer specifies a set of facts and rules, and an


engine infers the answers to questions.
Programming Paradigm

Constraint — Programmer specifies a set of constraints, and an engine infers the answers to
questions.
Aspect-Oriented — Programs have cross-cutting concerns applied transparently

Object-Oriented —
 Computation is effected by sending messages to
objects; objects have state and behavior.

Class-based —
 Objects get their state and behavior based on membership in a
class.
Introduction to Object-Oriented Programming

 Object-Oriented Programming : is a Programming paradigm or a software


development methodology in which a program is conceptualized as a group
of objects .
 A Programming technique in which a program is organized as a collection of
objects.

 A software development approach for writing and developing computer


programs based on objects.
Introduction to Object-Oriented Programming

OOP provides an easy of modeling things in the real


world.
OOP organizes & combines data and functions
together, hides and encapsulates the implementation
details and offers several features for developing
computer softwares.
Introduction to Object-Oriented Programming

 OOP : makes the development process easier faster and less time consuming.

 OOP features :
 Classes and Objects
 Data Encapsulation and Abstraction
 Inheritance

 Polymorphism

 And a programming language that supports or follows these features is called an


Object-Oriented Programming Language.
OOP Classes and Objects

 Two key concepts in OOP are :


 Objects and classes
An object is and entity that has certain
attributes or properties sometimes called
characteristics which may be assigned values.
The values can be numeric or non-numeric.
Introduction to Object-Oriented Programming

 An Analogy
 Examples of real world Objects
 In the real world everywhere we look we see objects such :
Introduction to Object-Oriented Programming

 An object represents a real world entity Students


Object
 Following figure shows some real-world entities
Book object
Introduction to Object-Oriented Programming

Book object
Attributes/Properties

BookName

AuthorName

Price

Edition
Introduction to Object-Oriented Programming

car object
Attributes

Name

Model

Price

Color
Introduction to Object-Oriented Programming

 Attributes are represented as variables in a class declaration.


Students
Attributes Object

Name

RollNo

Address
Introduction to Object-Oriented Programming

Attributes with values Students


Object

Name=“anyName”

RollNo=19Sw

Address=xyz
Introduction to Object-Oriented Programming

Attributes with values


car object

Name=Alto

Model=2017

Price=

Color=white
Introduction to Object-Oriented Programming

Attributes
car object
Name
Model
Price
Color

Behavior

Change gear()
Apply breaks()
Accelerate()
Introduction to Object-Oriented Programming

Attributes

Name  Behavior is equivalent to a method in a


RollNo program
Address

Behavior

setName()
changedept();
An Analogy
Classes : A class is a template for an object. Or
Classes are the definitions (or blueprints) Used to created objects.
The structure or Design of
a car
An Analogy

Object 1 Object 2
OOP Class and Object

Class: A class is a template for an object. OR


 Classes are the definitions (or blueprints) Used to created objects.
 Object : in general an object is an entity that has certain attributes or properties
which may be assigned values The values can be numeric or non-numeric.
 In OOP an Object is an instance of a class OR
an Object is a working copy of a class OR Look like a class.
 Objects in a program can represent real world things e.g. automobiles, Mobiles,
Books houses and employees.
OOP Class and Object

 There are just two kinds of things that you can include in a class definition:
 Fields: These are variables that store data items .They are also referred to as data members of a
class.
 The data, or variables, defined within a class are called instance variables.
 Methods: These define the operations you can perform for the class —so they determine what you
can do to, or with, objects of the class. Methods typically operate on the fields —the data members
of the class.

Collectively, the methods and variables defined


within a class are called members of the class.
OOP Class and Object

 Syntax
 class classname {
 type instance-variable1;
 type instance-variable2;
 type instance-variableN;
 type methodname1(parameter-list) {
 // body of method
 }
 // ...
 type methodnameN(parameter-list) {
 // body of method
 }
 } ;
Declaring a class with a method and Instantiating an
object of a class.

A Simple Class Use MyClass Class

class MyClass { int main () {

public : // access specifier // Creating an object of MyClass

void DispayMsg() MyClass MyObj; // Instantiating an object of MyClass


{
Cout<<“Welcome to Class 19SW-Section-I”; {
// calling MyObj’s method DisplayMsg()
}// end method
MyObj.DisplayMsg();
} ; // end class
return 0;

} // end main method


Declaring a class with Instance Variables
Use Students Class
A Simple Class
int main () {
class Students {
// Creating an object and asdign it to objStd
Private:
string student_rollno; // instance variable
Students objStd;
Public:
{
void set_rollno(string rollno)
// calling objStd’s method set_rollno()
{
student_rollno = rollno;
objStd. set_rollno(“17SW01");
} // end method
// calling objStd’s method get_rollno()
string get_rollno()
{
/ cout<<“Welcome”+ objStd. get_rollno());
return student_rollno;
} // end method
return 0;
} // end main method
} ; // end class
Each object (instance) of a class
contains/maintains it’s own copy of the class’s
instance variables and functions.
Primitive Types vs. Reference Types
Data types in C++ are divided into two categories
Primitive Types and Reference Types

Primitive Types are byte, int, char, Boolean, short, long, float and double.

A primitive variable can store exactly one value of its declared type at a
time.
Reference Types

All non primitive types are references types, so classes, we specify


the types of objects are reference types.

Variables of reference types (normally called references)


store the locations of objects in the computer’s memory.
Such variables are said to refer to objects in the program.
OOP set() & get() methods

variables declared inside a class but outside the


Instance variables:
bodies of class’s member functions.
(Set methods) & (get methods)
These are the public methods.
Allow the client of the class to set(i.e. assign values)
to the private instance variables , and
get (i.e. obtain values) of private instance variables.
OOP Access Specifiers

 Access Specifiers : Define level of Accessibility of a variable or a method OR


 Specify which part of the program an access a variable or a function.
 Private : variables or methods declared with access specifier private are accessible only within a
class in which they are declared.

 Public : variables or methods declared with access specifier public are accessible anywhere in the
program (i.e. within a class in which they are declared and also outside that class)
Protected : variables or methods declared with access specifier protected are accessible within a
class in which they are declared and also in the derived class).
OOP Constructor

 A Constructor : is a special member function which is called


automatically whenever an object of class is created.
 When an object of class is created, the constructor of that class is called.
 Is used to Initialize an object, and to initialize the instance variables.
 A constructor has two special characteristics that differentiate it from
other class methods:
 A constructor always has the same name as the class
 A constructor never returns a value, and you must not specify a return
type —not even of type void.
A Simple Class with Constructor

class Students {
private: int main () {
string studentName; // instance variable
// Creating an object
public:
Students () //Constructor Students objStd;
{
studentdName=“anyName”) ; } {
// calling objStd’s method getName()
void setName(String stdName) cout<<“Initial Value:”+ objStd.getName());
{
studentName=stdName; objStd.setName(“Ahmed");
} // end method cout<<“Welcome”+ objStd.getName());
public String getName() return 0;
{
return studentName; } // end main method
} // end method

}; // end class
OOP Constructors

 No-argument constructor initializes data members to constant values,

 A multi-argument constructor can initialize data members to values


passed as arguments,

 A default copy constructor initializes an object with an other object of


the same type.
 This is one argument constructor whose argument is an object of the
same class as the constructor.
OOP Destructor

 Destructor : Is a member function which is called automatically


whenever an object is destroyed.

 It has the same name as the constructor (which is the same as the class
name) but is preceded by a Tilde ~ .
 The most common use of destructor is to DE allocate the memory that
was allocate by the constructor for the object.
Introduction to Object-Oriented Programming

 Data Encapsulation & Abstraction


 Data Encapsulation or data hiding: Is an OOP feature that binds together data and functions. OR
 The wrapping of data and functions into a single unit. OR
 The internal of the object is are private to that object and may not be accessed from outside the object

 Abstraction: refers to the act of representing the functionality of a program and hide the internal details. OR
 A feature of OOP where by the implementation details of class are kept hidden from the user.
Data Encapsulation & Abstraction
Static class variables

 Each object (instance) of a class contains/maintains it’s own copy of the


class’s instance variables and functions.

 Static instance variables are not duplicated for each object, rather a single
data item (i.e. static member is shared by all objects of a class)
Structures and Classes

 Structures are the important building blocks in understanding of Objects and


classes. You can use structures in almost exactly the same way that you use
classes.
 The difference between class and struct are:
 A structure is collection of data, while a class is collection of both data and
functions.
 In a class the members are private by default, while in a structure they are
public by default.
 Classes are reference types, while structures are values types.
 http://www.differencebetween.info/difference-between-class-and-structure-in-
cplusplus
Tasks for Lab # 09
Task # 1 Create a class having 4 functions, add, sub, muland div. Each function accepts 2 parameters and returns the sum, difference,
multiplication and division of these numbers
Create a main class having main function that uses the above class.

Task # 2
Create a class called MyClass that has one int member. Include member functions to initialize it to 0, to initialize it to an integer
value, to display it.
Write a program to test this class.

Task # 3 Create an employee class. The member should comprise an int for storing the employee number, and float for the
employee’s salary. Member functions should allow the user to enter this data and display it. Write a main() function that allows the
user to enter data for three employees and display it.

Task # 4 Create a class that includes a data member that holds a “Serial number” for each object created from the class. That is, the
first object created will be numbered 1, the second 2, and so on.

Task # 5 Demonstrate the use of the following:


Constructor
Access specifiers private and public.
Set() and ge() functions.
static class members.

You might also like