Functions, & O.O.P.–C++ Notes (C Plus Plus Notes)
Functions
Performing a task in a program requires a function! The function hides from its user the complex tasks that it performs.
C++ Functions–A function is a block of code which only runs when it is called. Data, known as parameters, can be passed into a function.
Functions are used to perform certain tasks, or actions, and they are important for reusing code: Define the code once, and use it many times.
C++ provides some pre-defined functions, such as ‘main()‘ which is used to execute code. But you can also create your own functions to perform certain actions.
- C++ Functions consist of two parts:
- Declaration–the functions name, return type, and parameters, if any.
- Definition–the body of the function (code to be executed).
C++ What is OOP?
OOP–Object Oriented Programming
“You down with OOP?!!”
Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data & functions.
OOP has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs.
- OOP helps to keep the C++ code “D.R.Y.” (Don’t Repeat Yourself!) and makes the code easier to maintain, modify, & debug.
- OOP makes it possible to create full reusable applications with less code and shorter development time.
Tip: The “Don’t Repeat Yourself” principle is about reducing the repetition of code. You should extract out the codes that are common for the application & place them at a single place & reuse them instead of repeating it.
Typically, programs will consist of function ‘main’ and one or more classes, each containing data members & member functions. A development team in industry may work on software systems, that contain hundreds or even thousands of classes.
Develop a simple, well-engineered framework for organizing object oriented programs in C++.