Professional Documents
Culture Documents
ODS Lab4 Fundamentals of Object-Oriented Programming
ODS Lab4 Fundamentals of Object-Oriented Programming
Fundamentals of Object-Oriented
Programming
Objective:
Understand the object oriented programming paradigm.
Understand the declaration of classes.
Understand the data members, member functions and access specifiers.
Understand the use of constructor and destructor functions.
Understand the concept of encapsulation.
Understand the use of passing objects as function arguments.
In addition to attributes, all objects exhibit behaviors. A dog eats, barks, wags its tail, plays, and
begs. A dog exhibits many more other behaviors than this short list. It is a good idea to practice
listing attributes and behaviors of many of the objects you come across each day. Another thing
we need to remember about objects is that objects interact between each other.
Programmers, for over thirty years programmed using functions and procedures. Each function
and procedure was called to carry out certain tasks on the data that were given to it and to return
(or not return) certain results back. With this type of procedure oriented programming, we had to
adapt our thinking procedurally. Working with objects is a more normal and suitable approach for
human beings. Such programming approach is called Object Oriented Programming (OOP).
In Object Oriented Programming, objects are packages that contain data and functions (methods)
that can be performed on the data. Data could be considered to be attributes and functions are
Data abstraction is another important feature of Object oriented programming. OOP hides the
details of data structures from the users. For example, to set a date in the aforementioned example,
values of month, day and year are passed to the assign-date method. The actual representation of
the date is hidden from the user.
There can be various objects made of a particular class (recall that many variables can be made
from type). In OOP the general type with which objects can be made is called a class. An object is
an instance of a class. Each class contains data (data members) as well as set of functions (member
functions) that manipulate the data.
Defining Classes:
A class is defined in a similar way as a structure is defined. The keyword “class” is used to
define the class. The general syntax to define a class is:
class Class_name
{
Body of class
};
1. Components of a Class:
Data members
Member functions
Access modifiers
Constructors
Destructor
Variable that we define inside a class are called data members of class.
Department of Electrical Engineering 41
1.2. Member Functions
The members of a class that we don’t want to access from outside of the class are declared under
the private scope. We use private: keyword to specify the private scope. See (Figure 6.1).
All class members that come after the access specifier private: and up to the next access specifiers
are declared as private and are accessible only from inside the class. The default access mode is
private.
Note: if no access modifier is given, the data member is treated as private.
Restricting data members to be accessed from within the class is called “data hiding”, i.e. the data
members declared as private is hidden from outside the class. Data hiding is also known as
“encapsulation”.
Public members of a class can be accessed both from inside and from outside the class. We use
public: keyword to specify the public scope. See (Figure 6.1). Normally, member functions are
declared as public.
We use constructors to initialize the data members of the class. Constructor has no return type and
it is defined with the name of class in public scope as shown in (Figure 6.3).
There are two types of constructors
Default constructor
Parameterized constructor
Constructor which has not argument is called default constructor. We defined the default
constructor to initialize the data members of class with default values. See (Figure 6.3)
Constructor having one or more parameters is called parameterized constructor. See (Figure 6.3)
For example:
You can define all-purpose constructor for program (Figure 6.4) by this way
(Figure 6.4)
The above constructor will behave as default and parameterized both. You don’t need to define
constructor with no parameters.
If you have defined a parameterized constructor by this way and you never supply values to it then
the compiler will create a default constructor for you and initialized the data members with those
values which are assigned in parameters.
If you supply one value then it will assign the supplied value to first parameter and then set default
values to other parameters.
1.5. Destructor
Setter has normally void data type and one parameter with same data type for which it is declared.
Normally setters and getters are declared with the name of data member including “set or get” as
prefix, to increase the readability and understanding.
Objects of Class
Object is a variable of type class. We declare objects
with the name class because class is a data type. Object
is also known as instance of class. Each object consists
of both data members and the member functions of the
class. Combining the both data member and functions
into one unit is called data encapsulation. See (Figure
6.2).
Example:
#include<iostream>
using namespace std;
class Truck
{
private:
int number;
int model;
public:
// parameterized constructor
Truck(int _number=0, int _model=0)
{
number = _number;
model = _model;
}
//setters and getters
void setNumber(int _number)
{
number = _number;
}
int getNumber()
{
return number;
}
void setModel(int _model)
{
model = _model;
}
int getModel()
{
return model;
}
};
void main()
{
Truck truck1 (102 , 2014 , 30000); //supplied values to parameterized constructor
public:
Student()//default constructor
{
cgpa = 0;
}
// overloaded constructor
Student(char *_name, char *_address, float _cgpa)
{
name = _name;
address = _address;
cgpa = _cgpa;
}
//setters and getters
void setName(char *_name)
{
name = _name;
}
char* getName()
{
return name;
}
void setCgpa(float _cgpa)
{
cgpa = _cgpa;
}
float getCgpa()
{
return cgpa;
}
};
Activity 1.2:
Declare destructor for Student class and print “Object destroyed” using cout statement.
Run the program and see the output.
Activity 1.3:
Define all-purpose constructor for all data members of Student.
Call parameterized constructor with empty arguments and display all data
members on console.
Call parameterized constructor with one argument and display all data members
on console.
Call parameterized constructor by passing all arguments and display all data
members on condole.
Activity 1.4:
Declare another object “s2” of the same class and assign s1 to s2, then display all
information.
Activity 2.2:
Define a public member function calculateDuration in Time class that receive object
of type time and calculate the time difference to find out the duration and display on
screen.
Define following menu in main and get choice.
Practice Questions:
Question 1:
Create an Employee class with data members name, address, contactNo and designation in private
scope.
Define all-purpose parameterized constructor and two member function with name input and
display. Input function should get the input from user and should display all data members.
Define setters and getters of each data member.
Define main function. Declare 10 size array of type Employee
Ask the user to select choice from the following menu
Press 1 to add employee
Press 2 to display all employees
Press 3 to search employee
Press 4 to exit
Program should exit only when user select option 4.
Question 2:
Develop a banking system which provides facility to create accounts, display accounts
information, deposit amount, withdraw amount, and check balance.
To do this, you have to create a class which should have some data members and functions to
perform the given operations.
Your system also has a business rule that, to open a new account, each user has to deposit 500
hundred rupees.
Step 1:
Define a class Accounts with data members character array accTitle, character array accType, float
variable balance. All data members should be in private scope.
Step 2:
Define default constructor in public scope which initializes the data member balance to zero.
Refer Activity 1 code
Step 3:
Define parameterized constructor with parameters character array accTitle, character array
accType. Overloaded constructor should be in public scope.
Step 4:
Define setters and getters for each data member in public scope.
Step 5:
Define the main functions.
Declare one dimensional array of size 10 with data type Accounts.
Hint: Accounts acc[10];
If user enters input other than given option then program should display “wrong input” and then
display menu again.
Hint: use switch-case and do-while to achieve this task.
After defining all cases with proper syntax, perform the next step.
Step 7:
In the first case, you need to define logic for the creation of new account.
You have already declared an array of size 10 with data type Accounts.
Step 8:
In the second case, you need to define the logic to print the account information.
Don’t print information of all accounts.
You need to get account number as input from user and then print the information of that account.
Step 9:
In the third case, you need to define the logic to perform the deposit amount operation.
You need to get account number from user and then get amount to be deposit.
Step 11:
In fifth case, you need to define the logic to perform check balance operation.
You need to get account number from user and then display the balance on console.
Program Logic Program logic Program logic has Program logic is Program logic is
(20) has many errors some errors with mostly correct, but correct, with no
with majority of some contradictory may contain known errors, and
contradictory conditions occasional errors or no redundant
conditions redundant/ or contradictory
contradictory conditions.
conditions
Practical Makes several Makes few critical Makes some non- Applies the
Implementation critical errors in errors in applying critical errors in procedural
(30) applying procedural applying procedural knowledge in
procedural knowledge knowledge perfect ways
knowledge
Program Program does not Program approaches Program produces Program produces
Correctness produce correct answers or correct answers or correct answers or
(20) correct answers appropriate results appropriate results appropriate results
or appropriate for most inputs, but for most inputs. for all inputs
results for can contain tested.
most inputs. miscalculations in
some cases.
Use of Software Uses software Uses software tool, Uses software tool, Uses software tool,
Tool tool, with with some with considerable with a high degree
(10) limited competence competence of competence
competence
Marks Obtained
Total Marks: 40
Marks Obtained
LABORATORY SKILLS ASSESSMENT (Cognitive)
Total Marks: 10
( If any )
Marks Obtained