CS221_Note3
CS221_Note3
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
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
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;
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 }
};
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.