FUNC.CPP

• 1: #include <iostream.h>


• 2: int Add (int x, int y) {4:


• 3: cout << "In Add(), received " << x << " and " << y << "\n";


• 4: return (x+y); }


• 5: int main() { cout << "I'm in main()!\n";


• 6: int a, b, c;


• 7: cout << "Enter two numbers: ";


• 8: cin >> a;


• 9: cin >> b;


• 10: cout << "\nCalling Add()\n";


• 11: c=Add(a,b);


• 12: cout << "\nBack in main().\n";


• 13: cout << "c was set to " << c;


• 14: cout << "\nExiting...\n\n";


• 15: return 0; }


Output: 


• I'm in main()!
 
• Enter two numbers: 3 5
 
• Calling Add()
 
• In Add(), received 3 and 5
 
• Back in main().
 
• c was set to 8
 
• Exiting... 

Demonstrating call to a function

1: #include <iostream.h>
 
• 2: // function Demonstration Function
 
• 3: // prints out a useful message
 
• 4: void DemonstrationFunction()
 
• 5: { cout << "In Demonstration Function\n"; }
 
• 6: // function main - prints outa message, then
 
• 7: // calls DemonstrationFunction, then prints
 
• 8: //out a second message.
 
• 9: int main() { cout << "In main\n" ;
 
• 10: DemonstrationFunction();
 
• 11: cout << "Back in main\n";
 
• 12: return 0; } 

Output for Function call : 

• In main
 

• In Demonstration Function
 

• Back in main  

Using cout

Cout is a function as like as printf. We use cout in c++ and printf in C. Cout is easy to use. 

1: #include <iostream.h>
 
• 2: int main()
 
• 3: { cout << "Hello there.\n";
 
• 4: cout << "Here is 5: " << 5 << "\n";
 
• 5: cout << "The manipulator endl writes a new line to the screen." <<endl;
 
• 6: cout << "Here is a very big number:\t" << 70000 << endl;
 
• 7: cout << "Here is the sum of 8 and 5:\t" << 8+5 << endl;
 
• 8: cout << "Here's a fraction:\t\t" << (float) 5/8 << endl;
 
• 9: cout << "And a very very big number:\t" << (double) 7000 * 7000 << endl;
 
• 10: cout << "Don't forget to replace Jesse Liberty with your name...\n";
 
• 11: return 0;}

Output using cout: 

Hello there.
 
•Here is 5: 5
 
• The manipulator endl writes a new line to the screen.
 
• Here is a very big number: 70000
 
• Here is the sum of 8 and 5: 13
 
• Here's a fraction: 0.625
 
• And a very very big number: 4.9e+07
 
• Don't forget to replace Jesse Liberty with your name...
 • Jesse Liberty is a C++ programmer! 

58. Evolution of C++

• C was extended to create C++ by Bjarne Stroustrup


• Features needed to facilitate object-oriented programming was provided in C++


• C++ is the predominant language for commercial software development


• C++ is a superset of C. The leap from C to C++ is very significant


• Any legal C program is a legal C++ program.


• To get the full benefit of C++, programmers need to learn a new way of
conceptualizing and solving programming problems

57. Four Pillars of Object-Oriented Programming

* Encapsulation 
* Data Hiding
* Inheritance
* Polymorphism


C++ Fully supports object-oriented programming.

Encapsulation: -Being a self contained unit.

Data hiding: -An object can be used without knowing about its internal data members.

Encapsulation and Data Hiding:

* C++ supports encapsulation and data hiding through
* The creation of user-defined types, called classes.
* A well-defined class acts as a 


  -  Fully encapsulated entity--it is used as a whole unit.
  - The actual inner workings of the class are hidden.
  - Users of a well-defined class do not need to know how the class works.
  - They just need to know how to use it.

Inheritance and Polymorphism: 

* Inheritance

 
  - a new object type, which is an extension of an excising type can be declared
  - This new subclass is said to derive from the existing type and is sometimes called a derived type.
  - C++ supports reuse through inheritance.

* Polymorphism

 
  - refers to the same name taking many forms.
  - C++ Supports the idea that different objects do "the right thing" through.
  - Function polymorphism and class  polymorphism
free counters