SlideShare a Scribd company logo
Object Oriented Programming
CIT743
CIT 743
1
lecture notes
introduction
Background
CIT 743
2
 In a Procedural Programming (PP), program is written in a step by
step approach. At the end of the program or subroutine, tasks get
executed in a sequential manner
 PP focuses on breaking down a programming task into a collection of
variables, data structures and subroutines where the later two can
act on as many variables declared in the program.
 Variables can easily be modified by any member of a program
 Object Oriented Programming (OOP) focuses on breaking down a
task into units known as objects where each one includes its own
variables (data) and subroutines (methods).
 PP uses subroutines and other members to act on program data
structures whereas in OOP, object subroutines act on object data
CIT 743
3
 Nomenclature varies between the two paradigms but have the same
semantics
 In OOP, each object is capable of receiving messages, processing
data and sending messages to other objects
PP OOP
variable attribute
function method
argument message
module object
Advantages of OOP
CIT 743
4 OOP simulates real world objects thus providing easy understanding
and visualization of problems as well as the designs of the solutions.
Complexity of problems is reduced, program structures become clear.
 Easy modification (maintenance) of the system. This is because objects
are interacting through public interfaces allowing modifications in their
implementations without affecting the overall performances of the
system provided that modifications do not affect their functionalities.
 Modularity allows scalability of the system. More functionalities of the
system can be easily added at any time if modeled in form of objects.
 Reusability of code is practiced at highest level with OOP. Objects can be
reused as many times as needed and can also be used to solve similar
problems.
 Security of the program is much enhanced due to limitations of object
data from being accessed by other objects.
CIT 743
5
concepts
object & class
CIT 743
6
 Using OOP ;
o The overall program is made up of lots of different self-contained
components (objects),
o each object has a specific role in the program
o all objects can talk to each other in predefined ways.
Overview of Object and Class
 Object is the smallest element of a program designed to simulate a
real world object presented by the problem.
 A given problem can be broken down into unlimited number of
objects.
 Each object is designed and coded independently according to what
they actually represented in a real world.
 To deliver the overall program task, objects communicate through
messages.
CIT 743
7
 Objects have a standard structure; must have attributes and methods
(functions).
 Attributes/variables/data define specifications of objects while
methods define the functionalities offered by objects. Methods are
said to explain behaviors of objects.
 Methods make use of attributes of the same object to define different
behaviors of the object.
 For example
 Dog
 Attributes – name, age, colour, breed, etc.
 Behaviour – eat, go for a walk, wiggle tail, etc.
 Car
 Attributes – make, engine size, colour, gear system, speed, etc
 Behaviour – change gear, change speed, stop, etc.
 Person
 ???
CIT 743
8
 Class is an abstract of objects or can be also defined as a collection of
objects possessing similar general properties.
 Classes have similar structure as that of objects, the difference
between them being the degree of specification.
 Attributes of objects carry specific values while those of classes are
assigned to either general or default ones.
 Several objects can belong to the same class.
CIT 743
9
Classes are like “templates” for a particular set of objects
CIT 743
10
 A class is a generic template for a set of objects with similar features.
 Instance of a class = object
 If class is the general (generic) representation of an object, an
instance is its concrete representation.
 Another way of distinguishing classes from objects is:
 A class exists at design time, i.e. when we are writing OO code to
solve problems.
 An object exists at runtime when the program that we have just
coded is running.
CIT 743
11
 Example – address book. For each person we want to store:
 Name
 Age
 Address
 Phone number
 We can write a class called Person which will represent the abstract
concept of a Person.
 We will then be able to create as much Person objects as we like in
order to model our address book.
 All of these objects will be an instance of our Person class.
Class Structure
CIT 743
12//basic class construct
class ClassName {
//Attributes (identity section)
//Methods (behavior section)
}
Attributes
CIT 743
13
 The type of the attributes could be
 Any of the primitive types – int, double, boolean, …
 Previously defined classes in the C++ libraries e.g String
 Previously user-defined classes
 We can use as many attributes as we wish
 more attributes = more complex class
 Example:
Person – name, age, address, DOB, phoneNo, ppsNo, …
Methods
CIT 743
14
 Methods (subroutines –typically functions)
 a way of dividing larger programs into many smaller more
manageable segments of code each having it’s own specific task
 methods performs tasks independently of each other
 allows us to modularise the program
 Advantages
 More manageable programs
 Software reusability
CIT 743
15
 Each method has
 parameters (arguments)
 The parameters are local variables (accessible only within the
method)
 return type
 returns a value (or void)
CIT 743
16
How to define class in C++
Alternative 1: define methods implementations within the class
class Rectangle {
float width, height, area;
public:
void set_values (float a, float b) {
width = a; height = b;
}
void calcArea () {
area = width * height;
}
float getArea() {
return area;
}
};
CIT 743
17
Alternative 2: define methods implementations outside the class
class Rectangle {
float width, height, area;
public:
void set_values (float, float);
void calArea () ;
float getArea();
};
void Rectangle::set_values (float a, float b) {
width = a; height = b; }
void Rectangle::calArea () {
area= width * height; }
float getArea() {
return area; }
CIT 743
18
Most classes define a set of “set” and “get” methods to access and
modify the class variables (accessor and mutator methods)
class Person {
//attributes
string name;
int age;
string address;
string phoneNo;
// methods
public:
void setDetails (string newName, int newAge, string newAddress,
string newPhoneNo) {
name = newName; age = newAge;
address=newAdress;phoneNo=newPhoneNo;
}
CIT 743
19
string getName () {
return name;
}
string getAddress () {
return address;
}
};
How are we going to return age and phoneNo?
CIT 743
20
 Incorporating a class in a program
Example 1
//The program gets user’s values of width and height, calculates
rectangle //area then displays the area
#include <iostream>
using namespace std;
class Rectangle {
float width, height, area;
public:
void set_values (float a, float b) {
width = a; height = b; }
void calcArea () {
area = width * height; }
float getArea() {
return area; }
};
CIT 743
21
int main() {
float w,h;
//creating an object of Rectangle
Rectangle rect;
cout<<“Enter Rectangle width:”;
cin>>w;
cout<<“Enter Rectangle height:”;
cin>>h;
//Assigning user inputs to attributes
rect.set_values(w,h);
rect.calArea();
cout<<“Area of the Rectangle is:” << rect.getArea();
system(“pause”);
return 0;
}
CIT 743
22
Example 2
#include <iostream>
#include <string>
using namespace std;
class Person {
string name, address, phoneNo;
int age;
public:
void setDetails (string newName, int newAge, string newAddress,
string newPhoneNo) {
name = newName; age = newAge;
address=newAdress;phoneNo=newPhoneNo;
}
string getName () {
return name; }
string getAddress () {
return address; }
CIT 743
23
int getAge () {
return age; }
string getPhone () {
return phoneNo; }
};
int main() {
string newName, newAddress,NewPhoneNo;
int newAge;
Person p;
cout<<“Enter Name:”;
cin>> newName;
cout<<“Enter Address:”;
cin>>newAddress;
cout<<“Enter Age:”;
cin>>newAge;
CIT 743
24
cout<<“Enter Phone No:”;
cin>>newPhoneNo;
p.setDetails(newName, newAge, newAddress, newPhoneNo);
cout<<“Name of the person is:” << p.getName();
cout<<“n Address is:” << p.getAddress();
cout<<“n Age is:” << p.getAge();
cout<<“n Phone No is:” << p.getPhone();
system(“pause”);
return 0;
}
CIT 743
25
Example 3
What if we have more than one rectangle? Lets say two
#include <iostream>
using namespace std;
class Rectangle {
float width, height, area;
public:
void set_values (float a, float b) {
width = a; height = b; }
void calcArea () {
area = width * height; }
float getArea() {
return area; }
};
CIT 743
26
int main() {
float w,h;
Rectangle rect1, rect2;
cout<<“Enter width of Rectangle 1:”;
cin>>w;
cout<<“Enter height of Rectangle 1 :”;
cin>>h;
rect1.set_values(w,h);
rect1.calArea();
cout<<“Enter width of Rectangle 2:”;
cin>>w;
cout<<“Enter height of Rectangle 2 :”;
cin>>h;
rect2.set_values(w,h);
rect2.calArea();
CIT 743
27
cout<<“Area of the Rectangle 1 is:” << rect1.getArea();
cout<<“Area of the Rectangle 2 is:” << rect2.getArea();
system(“pause”);
return 0;
}
Encapsulation
CIT 743
28
 This refers to the hiding of class data and its implementation details
from being exposed to objects of other classes and making data
available only through defined methods (interface).
 This aims at avoiding accidental or deliberate damage of either class
data or implementations.
 Allows easy modifications and testing of internal structure of a class
without affecting how users use the class as long as class outputs are
retained.
 Encapsulation is achieved through access modifiers which commonly
are private , public and protected.
CIT 743
29
Public;
 Accessible anywhere with elements from either the same class or not.
Private;
 Accessible to only elements within the same class.
 Protected will be discussed in inheritance.
Constructors
CIT 743
30
 When object of a class is created, compiler calls for constructor for
that class. If no one was defined by programmer, compiler invokes a
default constructor it has created which only allocates memory for
the object but does not initialize attributes.
 The purpose of a user-defined constructor is to initialize attributes of
an object which later can be changed to the desired specifics of that
object. Initialization avoids risks of the program in assigning garbage
(or inconsistent) values to attributes which might develop serious
bugs.
 Constructor is closely similar to function with the exceptions that no
return type nor return statement. It is defined as just another
method of the class.
 Name of the constructor should be the same as that of the class
CIT 743
31
 There are two types of user defined constructors; constructor with no
parameters and the one with parameters.
 The first one, attributes values are determined within the class
whereas in the second one, values are determined outside the class
and therefore need to be passed as parameters.
 Constructor is called when new object is created.
 Consider the class below demonstrating the two constructors ( but
only one should be used in practical cases)
CIT 743
32
Example 1
class Point {
int x,y;
public:
Point() { // constructor with no parameter
x=0;
y=0;
}
Point(int new_x,int new_y) { // constructor with parameters
x=new_x;
y=new_y;
}
int getX() {
return x; }
int getY() {
return y; }
};
CIT 743
33
......in the main function
Point p; //parameterless constructor is called
Point q(10,20); //constructor with no parameter is called
CIT 743
34
Example 2
……………
Class Rectangle {
float width, height, area;
public:
Rectangle() { //user defined constructor with no parameters
width = 0;
height =0;
area=0; }
void set_values (float a, float b) {
width = a; height = b; }
void calArea () {
area = width * height;
}
CIT 743
35
float getArea () {
return area; }
};
int main () {
int w,h;
Rectangle rect;
cout <<“Specify width and height”;
cin>>w;
cin>>h;
rect.set_values (w,h);
rect.calArea();
cout <<“n Area is:”;
cout <<rect.getArea();
….
CIT 743
36
Example 3
……………
Class Rectangle {
float width, heigth, area;
public:
Rectangle(float p, float q) { //user defined constructor with
parameters
width = p;
heigth =q; }
void set_values (float a, float b) {
width = a; height = b; }
void calArea () {
area = width * height; }
float getArea () {
return area; }
};
CIT 743
37
int main () {
float w,h;
Rectangle rect(10.0,20.0);
cout <<“Specify width and height”;
cin>>w;
cin>>h;
rect.set_values (w,h);
rect.calArea();
cout <<“n Area is:”;
cout <<rect.getArea();
….
CIT 743
38
Example 4
#include <iostream>
using namespace std;
class Rectangle {
float width, height
public:
Rectangle (float a, float b) {
width=a; height=b;
}
float getArea() {
return (width * height);
}
};
CIT 743
39
int main () {
Rectangle recta (3.0,4.0);
Rectangle rectb (5.0,6.0);
cout << “recta area is: “ << recta.getArea() << endl;
cout << “rectb area is: “ << rectb.getArea() << endl;
…......
Array of objects
CIT 743
40
 Objects can be regarded as any other data types and therefore can be
saved in arrays.
Example 1
#include <iostream>
using namespace std;
class Rectangle {
int width;
int height;
public:
Rectangle() {
width = height = 0; }
void set(int w, int h) {
width = w;
height = h; }
int area() {
return (width * height); }
};
CIT 743
41
int itsAge;
int itsWeight;
};
int main()
{
Rectangle p[3];
p[0].set(3, 4);
p[1].set(10, 8);
p[2].set(5, 6);
for(int i=0; i < 3; i++) {
cout << "Area is " << p[i].area() << endl;
}
system(“pause”);
return 0;
}
 Note the declaration of arrays of object with defined max number of
objects to be stored.
CIT 743
42
Example 2
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass() {
itsAge = 1;
itsWeight=5;
}
int GetAge() {
return itsAge;
}
int GetWeight() {
return itsWeight;
}
void SetAge(int age) {
itsAge = age;
}
CIT 743
43
private:
int itsAge;
int itsWeight;
};
int main()
{
MyClass myObject[5];
int i;
for (i = 0; i < 5; i++) {
myObject[i].SetAge(2*i +1); }
for (i = 0; i < 5; i++) {
cout << " #" << i+1<< ": " << myObject[i].GetAge() << endl;
}
system(“pause”);
return 0;
}

More Related Content

What's hot (20)

OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Java Beans
Java BeansJava Beans
Java Beans
Ankit Desai
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Ramish Suleman
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
C# basics
 C# basics C# basics
C# basics
Dinesh kumar
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
File handling
File handlingFile handling
File handling
priya_trehan
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
rajveer_Pannu
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
Hawkman Academy
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Threads in python
Threads in pythonThreads in python
Threads in python
baabtra.com - No. 1 supplier of quality freshers
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
Arzath Areeff
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 

Similar to Ch.1 oop introduction, classes and objects (20)

OOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptxOOSD1-unit1_1_16_09.pptx
OOSD1-unit1_1_16_09.pptx
ShobhitSrivastava15887
 
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTSOOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
RajendraKumarRajouri1
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
Introduction to c ++ part -1
baabtra.com - No. 1 supplier of quality freshers
 
Oops
OopsOops
Oops
PRABHAHARAN429
 
C++ And Object in lecture3
C++  And Object in lecture3C++  And Object in lecture3
C++ And Object in lecture3
UniSoftCorner Pvt Ltd India.
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
sai kumar
 
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrrOOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
SanskritiGupta39
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
Hariz Mustafa
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++
Vaibhav Khanna
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Object Oriented Programming Class and Objects
Object Oriented Programming Class and ObjectsObject Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
rubini8582
 
Major terminologies in oop (Lect 2).ppt. Object oriented programming
Major terminologies in oop (Lect 2).ppt. Object oriented programmingMajor terminologies in oop (Lect 2).ppt. Object oriented programming
Major terminologies in oop (Lect 2).ppt. Object oriented programming
nungogerald
 
Oops ppt
Oops pptOops ppt
Oops ppt
abhayjuneja
 
12. MODULE 1.pptx
12. MODULE 1.pptx12. MODULE 1.pptx
12. MODULE 1.pptx
NIKHILAG9
 
object oriented programming language in c++
object oriented programming language in c++object oriented programming language in c++
object oriented programming language in c++
Ravikant517175
 
class c++
class c++class c++
class c++
vinay chauhan
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTSOOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
OOSD Lecture 1-1.pptx FOR ENGINEERING STUDENTS
RajendraKumarRajouri1
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
c++ lecture 1
c++ lecture 1c++ lecture 1
c++ lecture 1
sai kumar
 
c++ introduction
c++ introductionc++ introduction
c++ introduction
sai kumar
 
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrrOOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
OOPC_Final_U_I.pptx rrrrrrrrrrrrrrrrrrrrrr
SanskritiGupta39
 
Lecture01 object oriented-programming
Lecture01 object oriented-programmingLecture01 object oriented-programming
Lecture01 object oriented-programming
Hariz Mustafa
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++
Vaibhav Khanna
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Object Oriented Programming Class and Objects
Object Oriented Programming Class and ObjectsObject Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
rubini8582
 
Major terminologies in oop (Lect 2).ppt. Object oriented programming
Major terminologies in oop (Lect 2).ppt. Object oriented programmingMajor terminologies in oop (Lect 2).ppt. Object oriented programming
Major terminologies in oop (Lect 2).ppt. Object oriented programming
nungogerald
 
12. MODULE 1.pptx
12. MODULE 1.pptx12. MODULE 1.pptx
12. MODULE 1.pptx
NIKHILAG9
 
object oriented programming language in c++
object oriented programming language in c++object oriented programming language in c++
object oriented programming language in c++
Ravikant517175
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
Vineeta Garg
 
Ad

More from ITNet (20)

lecture 8 b main memory
lecture 8 b main memorylecture 8 b main memory
lecture 8 b main memory
ITNet
 
lecture 9.pptx
lecture 9.pptxlecture 9.pptx
lecture 9.pptx
ITNet
 
lecture 10.pptx
lecture 10.pptxlecture 10.pptx
lecture 10.pptx
ITNet
 
lecture 11.pptx
lecture 11.pptxlecture 11.pptx
lecture 11.pptx
ITNet
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
ITNet
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptx
ITNet
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
ITNet
 
kandegeeee.pdf
kandegeeee.pdfkandegeeee.pdf
kandegeeee.pdf
ITNet
 
Ia 124 1621324160 ia_124_lecture_02
Ia 124 1621324160 ia_124_lecture_02Ia 124 1621324160 ia_124_lecture_02
Ia 124 1621324160 ia_124_lecture_02
ITNet
 
Ia 124 1621324143 ia_124_lecture_01
Ia 124 1621324143 ia_124_lecture_01Ia 124 1621324143 ia_124_lecture_01
Ia 124 1621324143 ia_124_lecture_01
ITNet
 
Cp 121 lecture 01
Cp 121 lecture 01Cp 121 lecture 01
Cp 121 lecture 01
ITNet
 
Cp 111 5 week
Cp 111 5 weekCp 111 5 week
Cp 111 5 week
ITNet
 
Teofilo kisanji university mbeya (TEKU) ambassador 2020
Teofilo kisanji university mbeya (TEKU) ambassador 2020Teofilo kisanji university mbeya (TEKU) ambassador 2020
Teofilo kisanji university mbeya (TEKU) ambassador 2020
ITNet
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
ITNet
 
Tn 110 lecture 2 logic
Tn 110 lecture 2 logicTn 110 lecture 2 logic
Tn 110 lecture 2 logic
ITNet
 
Tn 110 lecture 1 logic
Tn 110 lecture 1 logicTn 110 lecture 1 logic
Tn 110 lecture 1 logic
ITNet
 
internet
internetinternet
internet
ITNet
 
Im 111 lecture 1
Im 111   lecture 1Im 111   lecture 1
Im 111 lecture 1
ITNet
 
development study perspective full
development study perspective fulldevelopment study perspective full
development study perspective full
ITNet
 
Gender issues in developement
Gender issues in developementGender issues in developement
Gender issues in developement
ITNet
 
lecture 8 b main memory
lecture 8 b main memorylecture 8 b main memory
lecture 8 b main memory
ITNet
 
lecture 9.pptx
lecture 9.pptxlecture 9.pptx
lecture 9.pptx
ITNet
 
lecture 10.pptx
lecture 10.pptxlecture 10.pptx
lecture 10.pptx
ITNet
 
lecture 11.pptx
lecture 11.pptxlecture 11.pptx
lecture 11.pptx
ITNet
 
lecture 12.pptx
lecture 12.pptxlecture 12.pptx
lecture 12.pptx
ITNet
 
lecture 13.pptx
lecture 13.pptxlecture 13.pptx
lecture 13.pptx
ITNet
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
ITNet
 
kandegeeee.pdf
kandegeeee.pdfkandegeeee.pdf
kandegeeee.pdf
ITNet
 
Ia 124 1621324160 ia_124_lecture_02
Ia 124 1621324160 ia_124_lecture_02Ia 124 1621324160 ia_124_lecture_02
Ia 124 1621324160 ia_124_lecture_02
ITNet
 
Ia 124 1621324143 ia_124_lecture_01
Ia 124 1621324143 ia_124_lecture_01Ia 124 1621324143 ia_124_lecture_01
Ia 124 1621324143 ia_124_lecture_01
ITNet
 
Cp 121 lecture 01
Cp 121 lecture 01Cp 121 lecture 01
Cp 121 lecture 01
ITNet
 
Cp 111 5 week
Cp 111 5 weekCp 111 5 week
Cp 111 5 week
ITNet
 
Teofilo kisanji university mbeya (TEKU) ambassador 2020
Teofilo kisanji university mbeya (TEKU) ambassador 2020Teofilo kisanji university mbeya (TEKU) ambassador 2020
Teofilo kisanji university mbeya (TEKU) ambassador 2020
ITNet
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
ITNet
 
Tn 110 lecture 2 logic
Tn 110 lecture 2 logicTn 110 lecture 2 logic
Tn 110 lecture 2 logic
ITNet
 
Tn 110 lecture 1 logic
Tn 110 lecture 1 logicTn 110 lecture 1 logic
Tn 110 lecture 1 logic
ITNet
 
internet
internetinternet
internet
ITNet
 
Im 111 lecture 1
Im 111   lecture 1Im 111   lecture 1
Im 111 lecture 1
ITNet
 
development study perspective full
development study perspective fulldevelopment study perspective full
development study perspective full
ITNet
 
Gender issues in developement
Gender issues in developementGender issues in developement
Gender issues in developement
ITNet
 
Ad

Recently uploaded (20)

How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdfHow to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
victordsane
 
Marketing And Sales Software Services.pptx
Marketing And Sales Software Services.pptxMarketing And Sales Software Services.pptx
Marketing And Sales Software Services.pptx
julia smits
 
SQL-COMMANDS instructionsssssssssss.pptx
SQL-COMMANDS instructionsssssssssss.pptxSQL-COMMANDS instructionsssssssssss.pptx
SQL-COMMANDS instructionsssssssssss.pptx
Ashlei5
 
Content Mate Web App Triples Content Managers‘ Productivity
Content Mate Web App Triples Content Managers‘ ProductivityContent Mate Web App Triples Content Managers‘ Productivity
Content Mate Web App Triples Content Managers‘ Productivity
Alex Vladimirovich
 
Design by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First DevelopmentDesign by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First Development
Par-Tec S.p.A.
 
GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...
GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...
GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...
GirikHire
 
Topic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptxTopic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptx
marutnand8
 
iOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod KumariOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod Kumar
Pramod Kumar
 
Top 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdfTop 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdf
LL Technolab
 
grade 9 ai project cycle Artificial intelligence.pptx
grade 9 ai project cycle Artificial intelligence.pptxgrade 9 ai project cycle Artificial intelligence.pptx
grade 9 ai project cycle Artificial intelligence.pptx
manikumar465287
 
aswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjar
aswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjaraswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjar
aswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjar
muhammadalikhanalikh1
 
Scalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple DevicesScalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple Devices
Scalefusion
 
Custom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdfCustom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdf
Digital Aptech
 
War Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona ToolkitWar Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona Toolkit
Sveta Smirnova
 
Agentic AI Desgin Principles in five slides.pptx
Agentic AI Desgin Principles in five slides.pptxAgentic AI Desgin Principles in five slides.pptx
Agentic AI Desgin Principles in five slides.pptx
MOSIUOA WESI
 
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptxHow AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
kalichargn70th171
 
Risk Management in Software Projects: Identifying, Analyzing, and Controlling...
Risk Management in Software Projects: Identifying, Analyzing, and Controlling...Risk Management in Software Projects: Identifying, Analyzing, and Controlling...
Risk Management in Software Projects: Identifying, Analyzing, and Controlling...
gauravvmanchandaa200
 
Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan
OnePlan Solutions
 
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternativesAI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative
 
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdfSecure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Northwind Technologies
 
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdfHow to purchase, license and subscribe to Microsoft Azure_PDF.pdf
How to purchase, license and subscribe to Microsoft Azure_PDF.pdf
victordsane
 
Marketing And Sales Software Services.pptx
Marketing And Sales Software Services.pptxMarketing And Sales Software Services.pptx
Marketing And Sales Software Services.pptx
julia smits
 
SQL-COMMANDS instructionsssssssssss.pptx
SQL-COMMANDS instructionsssssssssss.pptxSQL-COMMANDS instructionsssssssssss.pptx
SQL-COMMANDS instructionsssssssssss.pptx
Ashlei5
 
Content Mate Web App Triples Content Managers‘ Productivity
Content Mate Web App Triples Content Managers‘ ProductivityContent Mate Web App Triples Content Managers‘ Productivity
Content Mate Web App Triples Content Managers‘ Productivity
Alex Vladimirovich
 
Design by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First DevelopmentDesign by Contract - Building Robust Software with Contract-First Development
Design by Contract - Building Robust Software with Contract-First Development
Par-Tec S.p.A.
 
GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...
GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...
GirikHire Unlocking the Future of Tech Talent with AI-Powered Hiring Solution...
GirikHire
 
Topic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptxTopic 26 Security Testing Considerations.pptx
Topic 26 Security Testing Considerations.pptx
marutnand8
 
iOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod KumariOS Developer Resume 2025 | Pramod Kumar
iOS Developer Resume 2025 | Pramod Kumar
Pramod Kumar
 
Top 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdfTop 10 Mobile Banking Apps in the USA.pdf
Top 10 Mobile Banking Apps in the USA.pdf
LL Technolab
 
grade 9 ai project cycle Artificial intelligence.pptx
grade 9 ai project cycle Artificial intelligence.pptxgrade 9 ai project cycle Artificial intelligence.pptx
grade 9 ai project cycle Artificial intelligence.pptx
manikumar465287
 
aswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjar
aswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjaraswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjar
aswjkdwelhjdfshlfjkhewljhfljawerhwjarhwjkahrjar
muhammadalikhanalikh1
 
Scalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple DevicesScalefusion Remote Access for Apple Devices
Scalefusion Remote Access for Apple Devices
Scalefusion
 
Custom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdfCustom Software Development: Types, Applications and Benefits.pdf
Custom Software Development: Types, Applications and Benefits.pdf
Digital Aptech
 
War Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona ToolkitWar Story: Removing Offensive Language from Percona Toolkit
War Story: Removing Offensive Language from Percona Toolkit
Sveta Smirnova
 
Agentic AI Desgin Principles in five slides.pptx
Agentic AI Desgin Principles in five slides.pptxAgentic AI Desgin Principles in five slides.pptx
Agentic AI Desgin Principles in five slides.pptx
MOSIUOA WESI
 
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptxHow AI Can Improve Media Quality Testing Across Platforms (1).pptx
How AI Can Improve Media Quality Testing Across Platforms (1).pptx
kalichargn70th171
 
Risk Management in Software Projects: Identifying, Analyzing, and Controlling...
Risk Management in Software Projects: Identifying, Analyzing, and Controlling...Risk Management in Software Projects: Identifying, Analyzing, and Controlling...
Risk Management in Software Projects: Identifying, Analyzing, and Controlling...
gauravvmanchandaa200
 
Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan Delivering More with Less: AI Driven Resource Management with OnePlan
Delivering More with Less: AI Driven Resource Management with OnePlan
OnePlan Solutions
 
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternativesAI Alternative - Discover the best AI tools and their alternatives
AI Alternative - Discover the best AI tools and their alternatives
AI Alternative
 
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdfSecure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Secure and Simplify IT Management with ManageEngine Endpoint Central.pdf
Northwind Technologies
 

Ch.1 oop introduction, classes and objects

  • 1. Object Oriented Programming CIT743 CIT 743 1 lecture notes introduction
  • 2. Background CIT 743 2  In a Procedural Programming (PP), program is written in a step by step approach. At the end of the program or subroutine, tasks get executed in a sequential manner  PP focuses on breaking down a programming task into a collection of variables, data structures and subroutines where the later two can act on as many variables declared in the program.  Variables can easily be modified by any member of a program  Object Oriented Programming (OOP) focuses on breaking down a task into units known as objects where each one includes its own variables (data) and subroutines (methods).  PP uses subroutines and other members to act on program data structures whereas in OOP, object subroutines act on object data
  • 3. CIT 743 3  Nomenclature varies between the two paradigms but have the same semantics  In OOP, each object is capable of receiving messages, processing data and sending messages to other objects PP OOP variable attribute function method argument message module object
  • 4. Advantages of OOP CIT 743 4 OOP simulates real world objects thus providing easy understanding and visualization of problems as well as the designs of the solutions. Complexity of problems is reduced, program structures become clear.  Easy modification (maintenance) of the system. This is because objects are interacting through public interfaces allowing modifications in their implementations without affecting the overall performances of the system provided that modifications do not affect their functionalities.  Modularity allows scalability of the system. More functionalities of the system can be easily added at any time if modeled in form of objects.  Reusability of code is practiced at highest level with OOP. Objects can be reused as many times as needed and can also be used to solve similar problems.  Security of the program is much enhanced due to limitations of object data from being accessed by other objects.
  • 6. CIT 743 6  Using OOP ; o The overall program is made up of lots of different self-contained components (objects), o each object has a specific role in the program o all objects can talk to each other in predefined ways. Overview of Object and Class  Object is the smallest element of a program designed to simulate a real world object presented by the problem.  A given problem can be broken down into unlimited number of objects.  Each object is designed and coded independently according to what they actually represented in a real world.  To deliver the overall program task, objects communicate through messages.
  • 7. CIT 743 7  Objects have a standard structure; must have attributes and methods (functions).  Attributes/variables/data define specifications of objects while methods define the functionalities offered by objects. Methods are said to explain behaviors of objects.  Methods make use of attributes of the same object to define different behaviors of the object.  For example  Dog  Attributes – name, age, colour, breed, etc.  Behaviour – eat, go for a walk, wiggle tail, etc.  Car  Attributes – make, engine size, colour, gear system, speed, etc  Behaviour – change gear, change speed, stop, etc.  Person  ???
  • 8. CIT 743 8  Class is an abstract of objects or can be also defined as a collection of objects possessing similar general properties.  Classes have similar structure as that of objects, the difference between them being the degree of specification.  Attributes of objects carry specific values while those of classes are assigned to either general or default ones.  Several objects can belong to the same class.
  • 9. CIT 743 9 Classes are like “templates” for a particular set of objects
  • 10. CIT 743 10  A class is a generic template for a set of objects with similar features.  Instance of a class = object  If class is the general (generic) representation of an object, an instance is its concrete representation.  Another way of distinguishing classes from objects is:  A class exists at design time, i.e. when we are writing OO code to solve problems.  An object exists at runtime when the program that we have just coded is running.
  • 11. CIT 743 11  Example – address book. For each person we want to store:  Name  Age  Address  Phone number  We can write a class called Person which will represent the abstract concept of a Person.  We will then be able to create as much Person objects as we like in order to model our address book.  All of these objects will be an instance of our Person class.
  • 12. Class Structure CIT 743 12//basic class construct class ClassName { //Attributes (identity section) //Methods (behavior section) }
  • 13. Attributes CIT 743 13  The type of the attributes could be  Any of the primitive types – int, double, boolean, …  Previously defined classes in the C++ libraries e.g String  Previously user-defined classes  We can use as many attributes as we wish  more attributes = more complex class  Example: Person – name, age, address, DOB, phoneNo, ppsNo, …
  • 14. Methods CIT 743 14  Methods (subroutines –typically functions)  a way of dividing larger programs into many smaller more manageable segments of code each having it’s own specific task  methods performs tasks independently of each other  allows us to modularise the program  Advantages  More manageable programs  Software reusability
  • 15. CIT 743 15  Each method has  parameters (arguments)  The parameters are local variables (accessible only within the method)  return type  returns a value (or void)
  • 16. CIT 743 16 How to define class in C++ Alternative 1: define methods implementations within the class class Rectangle { float width, height, area; public: void set_values (float a, float b) { width = a; height = b; } void calcArea () { area = width * height; } float getArea() { return area; } };
  • 17. CIT 743 17 Alternative 2: define methods implementations outside the class class Rectangle { float width, height, area; public: void set_values (float, float); void calArea () ; float getArea(); }; void Rectangle::set_values (float a, float b) { width = a; height = b; } void Rectangle::calArea () { area= width * height; } float getArea() { return area; }
  • 18. CIT 743 18 Most classes define a set of “set” and “get” methods to access and modify the class variables (accessor and mutator methods) class Person { //attributes string name; int age; string address; string phoneNo; // methods public: void setDetails (string newName, int newAge, string newAddress, string newPhoneNo) { name = newName; age = newAge; address=newAdress;phoneNo=newPhoneNo; }
  • 19. CIT 743 19 string getName () { return name; } string getAddress () { return address; } }; How are we going to return age and phoneNo?
  • 20. CIT 743 20  Incorporating a class in a program Example 1 //The program gets user’s values of width and height, calculates rectangle //area then displays the area #include <iostream> using namespace std; class Rectangle { float width, height, area; public: void set_values (float a, float b) { width = a; height = b; } void calcArea () { area = width * height; } float getArea() { return area; } };
  • 21. CIT 743 21 int main() { float w,h; //creating an object of Rectangle Rectangle rect; cout<<“Enter Rectangle width:”; cin>>w; cout<<“Enter Rectangle height:”; cin>>h; //Assigning user inputs to attributes rect.set_values(w,h); rect.calArea(); cout<<“Area of the Rectangle is:” << rect.getArea(); system(“pause”); return 0; }
  • 22. CIT 743 22 Example 2 #include <iostream> #include <string> using namespace std; class Person { string name, address, phoneNo; int age; public: void setDetails (string newName, int newAge, string newAddress, string newPhoneNo) { name = newName; age = newAge; address=newAdress;phoneNo=newPhoneNo; } string getName () { return name; } string getAddress () { return address; }
  • 23. CIT 743 23 int getAge () { return age; } string getPhone () { return phoneNo; } }; int main() { string newName, newAddress,NewPhoneNo; int newAge; Person p; cout<<“Enter Name:”; cin>> newName; cout<<“Enter Address:”; cin>>newAddress; cout<<“Enter Age:”; cin>>newAge;
  • 24. CIT 743 24 cout<<“Enter Phone No:”; cin>>newPhoneNo; p.setDetails(newName, newAge, newAddress, newPhoneNo); cout<<“Name of the person is:” << p.getName(); cout<<“n Address is:” << p.getAddress(); cout<<“n Age is:” << p.getAge(); cout<<“n Phone No is:” << p.getPhone(); system(“pause”); return 0; }
  • 25. CIT 743 25 Example 3 What if we have more than one rectangle? Lets say two #include <iostream> using namespace std; class Rectangle { float width, height, area; public: void set_values (float a, float b) { width = a; height = b; } void calcArea () { area = width * height; } float getArea() { return area; } };
  • 26. CIT 743 26 int main() { float w,h; Rectangle rect1, rect2; cout<<“Enter width of Rectangle 1:”; cin>>w; cout<<“Enter height of Rectangle 1 :”; cin>>h; rect1.set_values(w,h); rect1.calArea(); cout<<“Enter width of Rectangle 2:”; cin>>w; cout<<“Enter height of Rectangle 2 :”; cin>>h; rect2.set_values(w,h); rect2.calArea();
  • 27. CIT 743 27 cout<<“Area of the Rectangle 1 is:” << rect1.getArea(); cout<<“Area of the Rectangle 2 is:” << rect2.getArea(); system(“pause”); return 0; }
  • 28. Encapsulation CIT 743 28  This refers to the hiding of class data and its implementation details from being exposed to objects of other classes and making data available only through defined methods (interface).  This aims at avoiding accidental or deliberate damage of either class data or implementations.  Allows easy modifications and testing of internal structure of a class without affecting how users use the class as long as class outputs are retained.  Encapsulation is achieved through access modifiers which commonly are private , public and protected.
  • 29. CIT 743 29 Public;  Accessible anywhere with elements from either the same class or not. Private;  Accessible to only elements within the same class.  Protected will be discussed in inheritance.
  • 30. Constructors CIT 743 30  When object of a class is created, compiler calls for constructor for that class. If no one was defined by programmer, compiler invokes a default constructor it has created which only allocates memory for the object but does not initialize attributes.  The purpose of a user-defined constructor is to initialize attributes of an object which later can be changed to the desired specifics of that object. Initialization avoids risks of the program in assigning garbage (or inconsistent) values to attributes which might develop serious bugs.  Constructor is closely similar to function with the exceptions that no return type nor return statement. It is defined as just another method of the class.  Name of the constructor should be the same as that of the class
  • 31. CIT 743 31  There are two types of user defined constructors; constructor with no parameters and the one with parameters.  The first one, attributes values are determined within the class whereas in the second one, values are determined outside the class and therefore need to be passed as parameters.  Constructor is called when new object is created.  Consider the class below demonstrating the two constructors ( but only one should be used in practical cases)
  • 32. CIT 743 32 Example 1 class Point { int x,y; public: Point() { // constructor with no parameter x=0; y=0; } Point(int new_x,int new_y) { // constructor with parameters x=new_x; y=new_y; } int getX() { return x; } int getY() { return y; } };
  • 33. CIT 743 33 ......in the main function Point p; //parameterless constructor is called Point q(10,20); //constructor with no parameter is called
  • 34. CIT 743 34 Example 2 …………… Class Rectangle { float width, height, area; public: Rectangle() { //user defined constructor with no parameters width = 0; height =0; area=0; } void set_values (float a, float b) { width = a; height = b; } void calArea () { area = width * height; }
  • 35. CIT 743 35 float getArea () { return area; } }; int main () { int w,h; Rectangle rect; cout <<“Specify width and height”; cin>>w; cin>>h; rect.set_values (w,h); rect.calArea(); cout <<“n Area is:”; cout <<rect.getArea(); ….
  • 36. CIT 743 36 Example 3 …………… Class Rectangle { float width, heigth, area; public: Rectangle(float p, float q) { //user defined constructor with parameters width = p; heigth =q; } void set_values (float a, float b) { width = a; height = b; } void calArea () { area = width * height; } float getArea () { return area; } };
  • 37. CIT 743 37 int main () { float w,h; Rectangle rect(10.0,20.0); cout <<“Specify width and height”; cin>>w; cin>>h; rect.set_values (w,h); rect.calArea(); cout <<“n Area is:”; cout <<rect.getArea(); ….
  • 38. CIT 743 38 Example 4 #include <iostream> using namespace std; class Rectangle { float width, height public: Rectangle (float a, float b) { width=a; height=b; } float getArea() { return (width * height); } };
  • 39. CIT 743 39 int main () { Rectangle recta (3.0,4.0); Rectangle rectb (5.0,6.0); cout << “recta area is: “ << recta.getArea() << endl; cout << “rectb area is: “ << rectb.getArea() << endl; …......
  • 40. Array of objects CIT 743 40  Objects can be regarded as any other data types and therefore can be saved in arrays. Example 1 #include <iostream> using namespace std; class Rectangle { int width; int height; public: Rectangle() { width = height = 0; } void set(int w, int h) { width = w; height = h; } int area() { return (width * height); } };
  • 41. CIT 743 41 int itsAge; int itsWeight; }; int main() { Rectangle p[3]; p[0].set(3, 4); p[1].set(10, 8); p[2].set(5, 6); for(int i=0; i < 3; i++) { cout << "Area is " << p[i].area() << endl; } system(“pause”); return 0; }  Note the declaration of arrays of object with defined max number of objects to be stored.
  • 42. CIT 743 42 Example 2 #include <iostream> using namespace std; class MyClass { public: MyClass() { itsAge = 1; itsWeight=5; } int GetAge() { return itsAge; } int GetWeight() { return itsWeight; } void SetAge(int age) { itsAge = age; }
  • 43. CIT 743 43 private: int itsAge; int itsWeight; }; int main() { MyClass myObject[5]; int i; for (i = 0; i < 5; i++) { myObject[i].SetAge(2*i +1); } for (i = 0; i < 5; i++) { cout << " #" << i+1<< ": " << myObject[i].GetAge() << endl; } system(“pause”); return 0; }