Constructors and Destructors:
Constructors: A constructor is a member function of a class which
initializes objects of a class. In C++, Constructor is
automatically called
when object (instance of class) create. It is special
member function of the class.
class A
{
public:
int x;
// constructor
A()
{
}
};
Destructors: Destructor is a member function which destructs or deletes an
object.
A destructor works opposite to constructor; it destructs the objects of
classes. It can be defined only once in a class. Like constructors, it is
invoked automatically.
class
A
{
public
:
// defining destructor for class
~A()
{
// statement
}
};
Types
of Constructors:
Constructors are of three types:
- Default Constructor
- Parametrized Constructor
- Copy COnstructor
1.Default Constructors:
Default
constructor is the constructor which doesn’t take any argument. It has no
parameters.
Syntax:
class_name(parameter1
,parameter2
,...)
{
// constructor Definition
}
2.Parameterized Constructors: It
is possible to pass arguments to constructors. Typically, these arguments help
initialize an object when it is created. To create a parameterized constructor,
simply add parameters to it the way you would to any other function. When you
define the constructor’s body, use the parameters to initialize the object.
class
Cube
{
public
:
int side
;
Cube(
int x
)
{
side
=x
;
}
};
int
main()
{
Cube
c1(10);Cube
c2(20);
Cube
c3(30);
cout
<<c1
.side
;
cout
<<c2
.side
;
cout
<<c3
.side
;
}
3.Copy Constructor:
A
copy constructor is a member function which initializes an object using another
object of the same class. Detailed article on Copy Constructor.
1.
class A
2.
{
3.
A(A &x) // copy constructor.
4.
{
5.
// copyconstructor.
6.
}
7.
}
Function:
A function is a block of code which only runs when it is called.
You can pass data, known as parameters, into a function.
Functions are used to perform certain actions, and they are
important for reusing code: Define the code once, and use it many times.
Create a Function
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.
To create (often referred to as declare) a function,
specify the name of the function, followed by parentheses ():
Call a Function
Declared
functions are not executed immediately. They are "saved for later
use", and will be executed later, when they are called.
To call a
function, write the function's name followed by two parentheses
()
and a semicolon ;
In the following
example,
myFunction()
is used to print a text (the action), when it is called:
Example
Inside
main
,
call myFunction()
:
// Create a
function
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
// Outputs "I just got executed!"
void myFunction() {
cout << "I just got executed!";
}
int main() {
myFunction(); // call the function
return 0;
}
// Outputs "I just got executed!"
Function Declaration and Definition
A C++ function consist of two
parts:
- Declaration: the function's name,
return type, and parameters (if any)
- Definition: the body of the function
(code to be executed)
Parameters and Arguments
Information can be passed to
functions as a parameter. Parameters act as variables inside the function.
Parameters are specified after the
function name, inside the parentheses. You can add as many parameters as you
want, just separate them with a comma:
Syntax
void functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
// code to be executed
}
Example
void myFunction(string fname) {
cout << fname << " Refsnes\n";
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}
cout << fname << " Refsnes\n";
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}
output:
Liam Refsnes
Jenny Refsnes
Anja Refsnes
Jenny Refsnes
Anja Refsnes
Default Parameter Value
You can also use a default
parameter value, by using the equals sign (
=
).
If we call the function without an
argument, it uses the default value ("Norway"):
Example
void myFunction(string country = "Norway") {
cout << country << "\n";
}
int main() {
myFunction("Sweden");
myFunction("India");
myFunction();
myFunction("USA");
return 0;
}
cout << country << "\n";
}
int main() {
myFunction("Sweden");
myFunction("India");
myFunction();
myFunction("USA");
return 0;
}
output:
Sweden
India
Norway
US
India
Norway
US
Types
of function:
1.Library
Function
Library functions are the built-in
function in C++ programming.
Programmer can use library function by
invoking function directly; they don't need to write it themselves.
Example 1: Library Function
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double number, squareRoot;
cout << "Enter a number: ";
cin >> number;
// sqrt() is a library function to calculate square root
squareRoot = sqrt(number);
cout << "Square root of " << number << " = " << squareRoot;
return 0;
}
Output
Enter a
number: 26
Square
root of 26 = 5.09902
2.User-defined Function:
C++ allows
programmer to define their own function.
A
user-defined function groups code to perform a specific task and that group of
code is given a name(identifier).
When
the function is invoked from any part of program, it all executes the codes
defined in the body of function
Example 2: User Defined
Function
C++ program
to add two integers. Make a function
add()
to add integers and display
sum in main() function.#include <iostream>
using namespace std;
// Function prototype (declaration)
int add(int, int);
int main()
{
int num1, num2, sum;
cout<<"Enters two numbers to add: ";
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << "Sum = " << sum;
return 0;
}
Output
Enters
two integers: 8
-4
Sum = 4
.
0 Comments