0% found this document useful (0 votes)
2 views

CS221_Note3

This document is a lecture note on Object Oriented Programming (OOP) in C++, covering complex data types, classes, objects, and key OOP principles such as encapsulation, inheritance, and polymorphism. It explains the structure of classes, the creation of objects, and the role of constructors in initializing class instances. Additionally, it provides examples illustrating the use of classes and member functions in C++.

Uploaded by

bankod20
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)
2 views

CS221_Note3

This document is a lecture note on Object Oriented Programming (OOP) in C++, covering complex data types, classes, objects, and key OOP principles such as encapsulation, inheritance, and polymorphism. It explains the structure of classes, the creation of objects, and the role of constructors in initializing class instances. Additionally, it provides examples illustrating the use of classes and member functions in C++.

Uploaded by

bankod20
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/ 5

25/06/2024

Content at glance
• This note covers the following topics
CS 221 • Complex data types
– Structures
Computer Programming II – Union
(Lecture Note 3: Complex data types & OOP) – records
By
Isma’il Aliyu • Object Oriented Programming in C++
Department of Mathematical Sciences – Objects & classes
Abubakar Tafawa Balewa University, Bauchi
– Inheritance

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI

Object Oriented Programming (OOP) OOP


• Object Oriented Programming is a programming paradigm • Usually, a given programming language is targeted for a
alongside the following specific application domain. For eg, system programming,
• Procedural (imperative) paradigm Web etc.
• Functional programming paradigm
• C++, however, is not confined to any specific application
• Logic Programming paradigm
domain and it supports many useful programming
• What is programming paradigm? paradigms
• A programming paradigm defines the methodology of designing and
implementing software, the program structure and how the problems • As stated earlier, C++ is a multi-paradigm language meaning
are generally analysed and solved. that it supports more than one paradigms.
• A programming language provides the linguistic means (keywords, • Procedural paradigm
constructs and syntax governing their use) as well as the extra- • Object Oriented Programming paradigm
linguistic capabilities, like standard libraries and programming
environment, to support a specific programming paradigm. • OOP enable the creation of user-defined types (objects),
and bundle data and meaningful operations in a single
entity called class.

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI


25/06/2024

OOP Class?
• In a nutshell, object oriented programming • A class is a unit of program that contain executable
statements organised in sub-units (functions or
(OOP) is style of programming involving methods).
classes and objects. • A class houses data (fields) and functions that perform
• Object oriented program is composed of one the class’s task.
or more class and set of objects. • C++ classes bundle data declarations with function
declarations, thereby coupling data with behavior.
• OOP has the following principles. • Access modifiers; eg, public or protected or private are
– Encapsulation (information hiding) used as part of class members (fields, & functions) to
control their visibility. That is, information hiding.
– Inheritance – This is the concept of information hiding also called
– Polymorphism encapsulation

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI

Object? Creating Object


• Once a class is created, it can be used. • Objects are sometimes called instance variables.
• To access the class’s content (functions & data), • To create an object of a class, here is the syntax. You
its object must be created. can use any one convenient for you.
– className object-name;
• Object is defined as an instance of a class. – ClassName objectName (parameters)
• Defining or creating objects is similar to defining – className objectName = className(parameters)
a variable of any data type. • Examples; suppose ABC is class with different
– Creating objects of a class is also called instantiation constructors, its objects can be created as follows:
• The term instantiating arises because an instance – ABC o1;
of the class is created. – ABC o2(2,5);
– ABC o3 = ABC (3,4);
CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI
25/06/2024

Class Members Declaring/Defining a class


• The class or struct type allows the programmer to • When you define a class, • an opening brace, and then
aggregate components into a single named variable. you define a blueprint for a lists the data members and
data type. member function of that
• What can be found in a class? • This doesn't actually define class. End the declaration
– A class has components, called members that are any data, but it does define with a closing brace and a
what the class name means, semicolon.
individually named.
that is, what an object of • The syntax of declaring a
• The class members are 3: the class will consist of and class is:
– Data members (fields) – variables declared outside what operations can be class ClassName {
functions. These variables are global all the functions in performed on such an // member 1
the class object. // member 2
….
• To declare a class, use the
– Member Functions (functions) class keyword followed by
// member n;
};
– Constructors the class name,

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI

Class members – functions & fields Constructor


• Functions • A constructor is a special member function that has the same name
as its class, which initializes objects.
– Performs a particular task.
• Constructor is automatically invoked or called when an object
– We treated functions. Please refer to note #1 (instance) of a class is created.
– They are divided into 2; • A class can have many constructors. But they must have different
• Accessor function – returns instance variables parameters.
• Mutator functions – changes or modify instance variables • A constructor does not have return type.
• Fields • There are 2 types of constructors:
– Are the variables declared outside functions. The syntax of – default constructor – a constructor that does not accept any
argument. If you create a class without explicitly creating a
instance field declaration is: constructor, compiler generates a default constructor which is invoked
• accessModifier: fieldName; OR automatically when objects are created but doesn’t perform any
• accessmodifier: fieldName = value; initialization.
– Accessmodifier can be any of these keywords: public, – parametered constructors – accepts parameters
private, protected

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI


25/06/2024

Creating Constructor Example 1


public:
• The syntax of creating constructor is: class ABC { void displayS(){
cout<<“Sum of X and Y = ”<<
private:
Class className { int x = 0, y = 0; }
x+y<<endl;

accessModifier: };
public:
int main(){
className (parameters){ ABC (){
x = 1; y = 1; ABC obj1(); // call default construc
//… } obj1.displayS();
ABC (int a){
} X = a; y = a;
ABC obj2 = ABC(5); // call construct 1
} obj2.displayS();
} ABC(int a, int b){
X = a; y = a;
ABC obj3(6,7); // call constructor 2
obj3.displayS();
• Where accessModifier can be any of the these }

}
return 0;

keywords: public, private, protected

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI

Example 1 Example 2
• In the above example, a class ABC • Which initializes each of the •
is created. instance fields to 1. #include <iostream> public: void setSpec(string m, int
• ABC class has • The second object obj2 is created #include <string> cn, float c) {
– two instance fields: x and y. by passing one argument model= m;
– Two parameterized constructors /parameter. In this case, the first using namespace std;
chasisnumber = cn;
and a default constructor. constructor is called which cost = c;
– One function, when invoke displays initializes the instance fields to class Car {
the sum of the values in instance the same value (parameter) }
// instance fields
fields x and y. passed private:
• In the main function. We • Similarly, the third object obj3 string model; public: string getInfo() {
instantiated the ABC class. invoke the second constructor int chasisnumber; string info;
• The first object is obj1 which is with 2 parameters which float cost; info = “Model: “ + model + “\n
created without passing any initializes x and y respectively Chasis Num: ”+ chasisnumber +
parameter to the constructor. In “\n Cost: ”+ cost ;
that case, the default constructor return info;
is invoked }
};

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI


25/06/2024

Example 2
• The values of the instance
int main() { fields are set via a setSpec
// create object of Car class function. getInfo function
Car c1; returns a string which is the
// call member functions concatenation of the instance
c1.setSpec(“ EOD 2006”, 373, fields with their values.
756170.5);
string car_info = c1.getInfo(); • Note:
cout<< car_info << endl; – This example has one default
return 0; constructor but it is implicit. It
} is automatically called when
the state Car c1; is executed.
• In the above example, a class
Car is created. It has two
functions; getInfo and setSpec
and 3 instance fields and a
default constructor.

CS221_22/23_I. Aliyu. DMS,ATBU BAUCHI

You might also like