Data Structure and Algorithms based on the Object-oriented programming
(OOP) Concepts such as Classes, Objects, constructor, Methods, or member
functions (later will explain their differences in next Lab Manual),
inheritance, polymorphism etc. Having good command on these basic concepts will
help you to understands DSA concepts and Algorithms better because we will have
to code every single Data structure & Algorithm using the Concepts of OOP
using C++. That is why I have designed this manual according to the requirement
of DSA lab to perform future tasks without any problem.
.
Today’s Concept:
Methods & Functions
Will also cover the Subtopics:
Prerequisite
of this Lab is all those students who have basic concepts on C++ Programming
such as Syntax of Language, concept of functions, pointers, datatypes etc.
Visual Studio 2012 and onward version will be used for implementation and
programming tasks assigned in the Lab.
Method
is a function in the Object-oriented programming
(OOP) on the other hand’s functions are line of codes which we can use
anywhere in our program. This technique in programming helps us from writing useful
code portion again and again we just simply write code
in the module form and use same module where we need it help to make our code
look more simple and less complex also it enhance the readability of Code.
Methods
works same as a function on a data members where methods declared for instant
if a class has a method which use to print its data member value we can invoke
a method by creating class object and referencing class method using class
object we use dot (.) operator to reference class methods from the main
class/test class as below:
Object_of_class.method_of_class
Each
object can call or reference its own method which is the part of the class. Method
can have a any access specifier such as private, public, and protected (rule
will be same as implemented on properties or attributes)
Functions
on the other hand’s is a line of code which takes specific input and produce
the output based on the input passed by the user. It defined independently in
the program for instant you can have a prototype of the function before the main
function or you can give full explanation (body) of the function before the
main, main is also a function one important thing is by default A Function
is Public but method is private. It can be access anywhere in the main program
using it’s name for instant if you have a function name Sum(int, int ) which
take two integer values to compute sum you will have to pass two parameters
when you want to call that function as below:
Sum(2,3)
Function
have different return types and a syntax shown below as:
Return_type_of_funtion
name_of_function (parameter list)
{
Body of
the function;
}
Encapsulation is refer as Data hiding in OOP where we give or
allow access to the properties or attributes only to class methods. For example
we have a class name car which has a name, and number as a attribute and a
set_name(), set_number() class methods to set their values this is called data
hiding or encapsulation in c++.
To Implement Encapsulation we do the following
- Make all the data members/class propertise/attributes private.
- Create public setter and getter functions for each data member.
Example of Encapsulation
using Methods:
class Example_created{
private:
int
number_one_item;
public:
int
getNumber_one_item() const {
return number_one_item;
}
void
setNumber_one_item(int num) {
this->number_one_item
= num;
}
};
int main(){
Example_created obj;
obj.setNumber_one_item(100);
cout<<obj.getNumber_one_item()<<endl;
system("pasue");
return 0;
}
Note: SetNumber_one_item & getNumber_one_item also a data Methods of this class which is called by the objects of the class.
Lab Activity Tasks:
- Create a program in C++ to computer the second Largest number from the integer array using functions. you function will return the value to the main function then you have to print the whole array's values in the main and also second largest number on the console.
- Create a program in C++ to print the Series as below using Functions:
2,3,5,9,17,33,65,129....
use your ability to think for making logic for this program.
- Create a program in C++ and implement Encapsulation concept which we have learnt in our lab Session using C++ and data methods.
Post a Comment