Programming Fundamentals: Practical#10)
Programming Fundamentals: Practical#10)
in
C++
(PRACTICAL#10)
Object Oriented Programming
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
OOP : makes the development process easier faster and less time consuming.
OOP features :
Classes and Objects
Data Encapsulation and Abstraction
Inheritance
Polymorphism
An Analogy
Examples of real world Objects
In the real world everywhere we look we see objects such :
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
Name
RollNo
Address
Introduction to Object-Oriented Programming
Name=“anyName”
RollNo=19Sw
Address=xyz
Introduction to Object-Oriented Programming
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
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
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.
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.
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
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
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
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
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
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
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.