0% found this document useful (0 votes)
36 views10 pages

C - unit-IV

FUNDSMENTALS OF C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views10 pages

C - unit-IV

FUNDSMENTALS OF C++
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

1 OOPS Concepts

1) OOPS Features:-The prime purpose of C++ programming was to add object orientation to
the C programming language, which is in itself one of the most powerful programming
languages.
The core of the pure object-oriented programming is to create an object, in code, that has certain
properties and methods. For example a car is an object which has certain properties such as
color, number of doors, and the like.
There are a few principle concepts that form the foundation of object-oriented
programming −

Object:-This is the basic unit of object oriented programming. That is both data and function
that operate on data are bundled as a unit called as object.

Class:-When you define a class, you define a blueprint for an object. This doesn't actually define
any data, but it does define what the class name means, that is, what an object of the class will
consist of and what operations can be performed on such an object.

Abstraction:-Data abstraction refers to, providing only essential information to the outside
world and hiding their background details, i.e., to represent the needed information in program
without presenting the details.

Encapsulation:-Encapsulation is placing the data and the functions that work on that data in the
same place. While working with procedural languages, it is not always clear which functions
work on which variables but object-oriented programming provides you framework to place the
data and the relevant functions together in the same object.

Inheritance:- Inheritance is the process of forming a new class from an existing class that is
from the existing class called as base class, new class is formed called as derived class.

Polymorphism:-The ability to use an operator or function in different ways in other words


giving different meaning or functions to the operators or functions is called polymorphism. Poly
refers to many.

Overloading:-The concept of overloading is also a branch of polymorphism. When the exiting


operator or function is made to operate on new data type, it is said to be overloaded.
2 OOPS Concepts

2)Structure of C++ program:-A C++ program is structured in a specific and particular


manner. In C++, a program is divided into the following three sections:

1. Standard Libraries Section


2. Main Function Section
3. Function Body Section

1.Standard libraries section:-

 #include is a specific preprocessor command that effectively copies and pastes the entire
text of the file, specified between the angle brackets, into the source code.

 The file <iostream>, which is a standard file that should come with the C++ compiler, is
short for input-output streams. This command contains code for displaying and getting
an input from the user.

 namespace is a prefix that is applied to all the names in a certain set. iostream file defines
two names used in this program - cout and endl.

 This code is saying: Use the cout and endl tools from the std toolbox.

2.Main function section:-

 The starting point of all C++ programs is the main function.

 This function is called by the operating system when your program is executed by the
computer.

 { signifies the start of a block of code, and } signifies the end.

3.Function body section:-

 The name cout is short for character output and displays whatever is between
the << brackets.

 Symbols such as << can also behave like functions and are used with the keyword cout.

 The return keyword tells the program to return a value to the function int main

 After the return statement, execution control returns to the operating system component
that launched this program.

 Execution of the code terminates here.


3 OOPS Concepts

3)Classes :-

A class definition starts with the keyword class followed by the class name; and the class
body, enclosed by a pair of curly braces. A class definition must be followed either by a
semicolon or a list of declarations. For example, we defined the Box data type using the
keyword class as follows −

class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that follows it.
A public member can be accessed from outside the class anywhere within the scope of the class
object. You can also specify the members of a class as private or protected which we will
discuss in a sub-section.

4)Objects:-

A class provides the blueprints for objects, so basically an object is created from a class.
We declare objects of a class with exactly the same sort of declaration that we declare variables
of basic types. Following statements declare two objects of class Box −

Box Box1; // Declare Box1 of type Box


Box Box2; // Declare Box2 of type Box
Both of the objects Box1 and Box2 will have their own copy of data members.
5)Friend Function:-
A friend function can be given a special grant to access private and
protected members. Following are some important points about friend functions and classes:

1) Friends should be used only for limited purpose. too many functions or external classes are
declared as friends of a class with protected or private data, it lessens the value of encapsulation
of separate classes in object-oriented programming.

2) Friendship is not mutual. If class A is a friend of B, then B doesn’t become a friend of A


automatically.

3) Friendship is not inherited

4) The concept of friends is not there in Java.


4 OOPS Concepts

Example:-
#include <iostream>
class B;
class A
{
public:
void showB(B&);
};
class B
{
private:
int b;
public : B( )
{
b = 0;
}
friend void A::showB(B& x); // Friend function
};
void A::showB(B& x)
{
std::cout << "B::b = " << x.b;
}
int main()
{
A a;
B x;
a.showB(x);
return 0;
}

Output:
B::b = 0
5 OOPS Concepts

6)Statistics Functions:- A function that is declared static using the ‘static‘ keyword
becomes a static function in C++.
Syntax of the Static Function:
static <return_type> <function_name>(<arguments>)
{ //code
}
When a function inside a class is declared as static, it can be accessed outside the class
using the class name and scope resolution operator (::), without creating any object A
static member method has access only to the static members of the class, we can not call
any non-static functions inside it.
Example:-
#include <iostream>
using namespace std;

class Nokia
{
private: static string phone_name;
public: static void name( )
{
cout << "Phone: "<< phone_name;
}
static void set_name(string name)
{
phone_name = name;
}
};
string Nokia::phone_name = "";
int main( )
{
Nokia::set_name("Nokia 2600");
Nokia::name();
return 0;
}
Output:
Phone: Nokia 2600
6 OOPS Concepts

7)Constructor:- Constructors are special class functions which performs initialization of every
object. The Compiler calls the Constructor whenever an object is created. Constructors initialize
values to object members after storage is allocated to the object.
Let's start with Constructors first, following is the syntax of defining a constructor function in a
class:

class A
{
public:int x;
constructor A( )
{
/ object initialization
}
};
8)Types of Constructors in C++:-Constructors are of three types:
1. Default Constructor
2. Parametrized Constructor
3. Copy COnstructor

i.Default Constructors:-Default constructor is the constructor which doesn't take any argument.
It has no parameter.

Syntax: class_name(parameter1, parameter2, ...)


{
// constructor Definition
}
For example:
class Cube
{
public: int side;
Cube( )
{
side = 10;
}
};
int main()
{
Cube c;
cout << c.side;
}
Output:-

10
7 OOPS Concepts

ii.Parameterized Constructors:-These are the constructors with parameter. Using this


Constructor you can provide different values to data members of different objects, by passing the
appropriate values as argument.

For example:

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;
}
Output:-

10

20

30

iii.Copy Constructor:-
Copy Constructors is a type of constructor which is used to create a copy of an
already existing object of a class type. It is usually of the form X (X&), where X is the class
name. The compiler provides a default Copy Constructor to all the classes.

Syntax of Copy Constructor

Classname(const classname & objectname)


{
. . . ..
}

As it is used to create an object, hence it is called a constructor. And, it creates a new object,
which is exact copy of the existing copy, hence it is called copy constructor.
8 OOPS Concepts

Below is a sample program on Copy Constructor:


#include<iostream>
using namespace std;
class Samplecopyconstructor
{
private: int x, y;
public:Samplecopyconstructor(int x1, int y1)
{
x = x1;
y = y1;
}
/* Copy constructor */
Samplecopyconstructor (const Samplecopyconstructor &sam)
{
x = sam.x;
y = sam.y;
}
void display()
{
cout<<x<<" "<<y<<endl;
}
};

int main()
{
Samplecopyconstructor obj1(10, 15);
Samplecopyconstructor obj2 = obj1;
cout<<"Normal constructor : ";
obj1.display();
cout<<"Copy constructor : ";
obj2.display();
return 0;
}
Output:-
Normal constructor : 10 15
Copy constructor : 10 15
9 OOPS Concepts

8)Destructor:-A destructor is a special member function that works just opposite to constructor,
unlike constructors that are used for initializing an object, destructors destroy (or delete) the
object.
Syntax of Destructor
~class_name()
{
//Some code
}
A destructor is automatically called when:
1) The program finished execution.
2) When a scope (the { } parenthesis) containing local variable ends.
3) When you call the delete operator.

Destructor Example
#include <iostream>
using namespace std;
class HelloWorld{
public: Hello World( )
{
cout<<"Constructor is called"<<endl;
}
~HelloWorld( )
{
cout<<"Destructor is called"<<endl;
}
void display( )
{
cout<<"Hello World!"<<endl;
}
};
int main( )
{
HelloWorld obj;
obj.display();
return 0;
}

Output:
Constructor is called
Hello World!
Destructor is called
10 OOPS Concepts

9)Unary Operator:-Unary operators are the operators that operates on single operand to give
the specific result. To perform the operations on these operators we need to use only single
operand.
Some of the unary operators available in C++ programming language as mentioned below:

1. Unary Plus:-A unary plus operator is denoted by symbol “+” and this operator doesn’t make
any changes on the operand value. It always represents the value of the operands.
Syntax: +

2. Unary Minus:-A unary minus operator is denoted by the symbol “-” and this operator makes
changes on the operand value and as a result, it makes the given value negative. Convert a
positive value to negative value and negative value to positive value. It always represents the
value of the operands.

Syntax: -

3. Increment operator:-The increment operator is denoted by symbol “++”. Increment


operators always increase the value by 1. Usually two type post-increment and pre-increment
operator.

Syntax: ++

4. Decrement operator:-Decrement operator is denoted by the symbol “–”.The decrement


operator always decreases the value by 1. Usually two type post-decrement and pre-decrement
operator.

Syntax: --

5. Address of the operator:-The address of the operator is denoted by the symbol “&” This
operator returns the address of any variable. As it usually takes the address of its operand. The
operand of the address of the operator can be a function or an Integer value that resides in an
object.

Syntax: &

6. Size of the operator:-The size of the operator is denoted by the symbol “sizeof()”. The size of
the operator acts like a function. This operator always returns the variable and object occupied
size. This operator also returns the size of any data types. It is also known as a compile-time
unary operator.

Syntax: sizeof( )

You might also like