0% found this document useful (0 votes)
10 views35 pages

C++first Unit

C++ important notes

Uploaded by

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

C++first Unit

C++ important notes

Uploaded by

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

TRIAL MODE − Click here for more information

C++ - 41 Marks
Data types in C++ - 3M
Data Types

Built In /Basic type User defined Derived

class Function
Integral structure Array
void Floating Point
Union Pointer
Enumeration

Char int float double


TRIAL MODE − Click here for more information

Enlist the basic datatypes in C++ with size of data in terms


of bytes-3M
Data Types Bytes Range
1)Integral
a)char
i)signed 1 -128 to +127
ii)unsigned 1 0 to 255
b)int
1)Short int
a)signed 2 -32768 to +32767
b)unsigned 2 0 to 65535
2)long int
a)signed 4 -2147483648 to +2147483647
b)Unsigned 4 0 to 4294967295
2)void -- ------
3)Floating point
a)Float 4 3.4x10-38 to 3.4x10+38
b)Double 8 1.7x10-308 to 1.7x10+308
i)long double 10 1.2x10-4932 to 1.2x10+4932
TRIAL MODE − Click here for more information

Explain call by value and call by reference with example-3/4 M


Call by value
a) When the portion of the program invokes a function, control
will be transferred from the main function to the calling
function and the value of actual arguments is copied to the
function.
b) Within function the actual value may be altered or changed.
c) When the control is transferred back from function to the
program, altered values are not transferred back. This type
of passing formal argument to a function is called as call by
value.
TRIAL MODE − Click here for more information

For example:
void fun( int x, int y); //function prototype
void main( )
{
----
fun(x,y); //call by value(function call)
----
}
void fun( int x, int y) // function definition
{
-----
}
TRIAL MODE − Click here for more information

Call by reference
• When a function is called by a program the address of the
actual arguments are copied on to the formal arguments
i.e the formal and actual arguments are referring to same
memory location.
• Therefore change in value of formal argument affects the
value of actual arguments.
• The content of a variable that are altered within the
function are returned to calling portion of a program in
the altered form.
TRIAL MODE − Click here for more information

For example:
void fun( int *x, int *y); //function prototype
void main( )
{
----
fun( x, y); //call by reference(function call)
----
}
void fun( int *x, int *y) // function definition
{
-----
}
TRIAL MODE − Click here for more information

Scope Resolution Operator(SRO) ::


In C++ ,Scope Resolution Operator (::) is used to access global
variable from a function in which a local variable is defined
with the same name as global variable.
Syntax- ::variablename;
TRIAL MODE − Click here for more information

Concepts of OOP(Object Oriented Programming-4M


Objects-Objects are the physical and conceptual things we find in
the universe around us. Objects are the basic runtime entities in
object oriented system. They are also called as class type
entities. They are even called as an instance of the class. They
are used to access data and functions in the class. eg. hardware,
software, person etc.
Classes-Class is said to be user defined data type which is defined
by the user. Class is a pattern, template or blueprint for a
category of structurally identical items. The items created using
class are called as instances. Once a class is defined user can
define any number of objects belonging to that class.
TRIAL MODE − Click here for more information

Encapsulation-The wrapping up of data and its associated


functions together under a single unit is called encapsulation.
Inheritance-It is defined as the process whereby one object
acquires characteristics from one or more other objects. The
mechanism of deriving a new class from the existing one is
called inheritance.
Polymorphism-Poly means many and morphism means forms.
Polymorphism means existing in many forms that is same
entity like function, operator is implemented in different
ways.
TRIAL MODE − Click here for more information

Data Abstraction/ Information hiding-Abstraction refers to


the act of representing essential features without including
the background details of explanations. Information hiding
is a process of hiding all the details of an object that do not
contribute to its essential characteristics.
Explain Class -3/4 M
Classes are data structures clubbing together data and
associated functions together.
The syntax of class declaration is given below
TRIAL MODE − Click here for more information

class classname(object)
{
access_specifier:
data and functions
access_specifier:
data and functions
access_specifier:
data and functions
};
class is a keyword, access specifier are public, private, protected which
decides scope and visibility of data members and member functions
declared in the class definition.
TRIAL MODE − Click here for more information

1)public access specifier or modifier:


All data members and member functions declared using
public access modifier are accessible by any function in the
program.
2)private access specifier or modifier:
If you don’t specify, any access modifier to the data
members or member functions of the class, they are by default
private. These are hidden from the outside world and they are
used to implement data hiding concept of Object Oriented
Programming. But only member functions and friends of the
class can use them in which they are declared.
TRIAL MODE − Click here for more information

3)protected access specifier or modifier:


These can only be used by member functions and friend of a
class as well as derived class member functions. They are
similar to private members since they can’t be accessed
directly by non member functions, but can be used by derived
ones. Therefore protected is more restrictive than public but
less restrictive than private.
TRIAL MODE − Click here for more information

Describe how member functions of class can be defined


outside and inside the class-4M
Member functions of class can be defined at two places:
1)Outside the class definition
2)Inside the class definition
1)Outside the class definition: The general form of member
function definition outside the class definition is:
Returntype classname::functionname(argument declaration)
{
//function body
}
TRIAL MODE − Click here for more information

e.g.
class try
{
public:
void display();//member function
};
//member function definition outside class
void try::display()
{
cout<<“\n C++Programming”;
}
TRIAL MODE − Click here for more information

2)Inside the class definition:


Another method for defining a member function is to replace the
function declaration by the actual function definition.
e.g.
class try
{
public:
void display()//member function
{
cout<<“\n C++ programming”;
} };
TRIAL MODE − Click here for more information

When a function is defined inside a class, it is treated as an inline


function. Normally only small functions are defined inside the class
definition.
Write a C++ program using OOP to find area of a circle. [5M]
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a; //data members
public:
TRIAL MODE − Click here for more information

void area() //member function


{
cout<<"Enter r";
cin>>r;
a=3.142*r*r;
cout<<"\n The area of circle="<<a;
}
};
void main() //non member function
{
clrscr();
circle Z; //instance of a class
Z.area(); // accessing member function via instance
getch();
}
TRIAL MODE − Click here for more information

Write a C++ program using OOP to find area of a circle.


[5M]
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a; //data members
public:
void area();//member function
};
TRIAL MODE − Click here for more information

void circle::area()
{
cout<<"Enter r";
cin>>r;
a=3.142*r*r;
cout<<"\n The area of circle="<<a;
}
TRIAL MODE − Click here for more information

void main() //non member function


{
clrscr();
circle Z; //instance of a class
Z.area(); // accessing member function via instance
getch();
}
TRIAL MODE − Click here for more information

Constructor and Destructor-3/4 M


Constructor
• A constructor is an operation that has the ability to
change the state of an object.
• The initialization takes place using special function
called constructor.
• The constructor is a function whose name is same as
the class but with no return type not even void.
• They are methods that are used to initialize an object
at its definition time.
TRIAL MODE − Click here for more information

• Constructors are implicitly called when we define objects of


their classes.
• There are 3 types of constructors.
1) Default constructor: If you do not define any constructor in
your class then system provides it. It has no body but you can
define it as well. It is also known as no argument constructor.
classname()
{
//body of default constructor
}
TRIAL MODE − Click here for more information

2) Parameterised constructor : This type of constructor


can take arguments so it is called parameterised
constructor.

classname(parameter list)
{
//body of parameterised constructor
}
TRIAL MODE − Click here for more information

3)Copy constructor
It is a constructor with only one parameter of its same
type that assigns to every nonstatic class member
variable of the object a copy of the passed object. It is
always used when the complier has to create a
temporary object of a class object.
Syntax: classname(classname var)
{
//body of copy constructor
}
TRIAL MODE − Click here for more information

Ex.
#include<iostream.h>
#include<conio.h>
class sample
{
int x;
public:
sample(int a)//parameterised constructor
{
x=a;
}
TRIAL MODE − Click here for more information

sample(sample i)
{
x=i.x;
}
void display()
{
cout<<"x="<<x<<"\t";
}
};
TRIAL MODE − Click here for more information

void main()
{
clrscr();
sample obj(5);
sample obj1(obj); //copy constructor invoked
obj.display();
obj1.display();
getch();
}
Output: x=5 x=5
TRIAL MODE − Click here for more information

Destructor
A mechanism which automatically destroys an object
when it gets invalid. Destructors are used to release any
resources allocated by the object’s constructor. They are
automatically for you and there’s only one destructor for
each object. The name of the destructor is the same as
class name but preceded by tilde(~). They take no
arguments.
Ex.
#include<iostream.h>
#include<conio.h>
TRIAL MODE − Click here for more information

class ratio
{
public:
ratio() //constructor
{
cout<<“\n The object is born”;
}
~ratio() //destructor
{
cout<<“\n The object dies”;
}
TRIAL MODE − Click here for more information

void ratiox() // member function


{
cout<<“\n Now X is alive”;
}
};
void main()
{
clrscr();
ratio Z;
Z.ratiox();
getch(); }
TRIAL MODE − Click here for more information

Inheritance- The mechanism of deriving a new class from the existing


one is called inheritance. The existing class is called base class and the
new class is called derived class. It supports the concept of reusability.
The syntax of derived class is given below:
class derivedclassname:access specifier baseclassname
{
//body of class
}
There are 5 types of inheritance
1)Single Inheritance -
A derived class with only one base class is called Single Inheritance.
TRIAL MODE − Click here for more information

Base class

Derived class

2)Multiple Inheritance-
When a class is derived from several base classes it is called as
multiple Inheritance.
Base 1 Base 2 Base n

Derived class
TRIAL MODE − Click here for more information

3)Multilevel Inheritance
The mechanism of deriving one class from another derived
class is called multilevel Inheritance.
Base class

Derived 1

Derived n

4)Hierarchical Inheritance
When several derived classes are derived from a single base
class then it is called as Hierarchical Inheritance.
TRIAL MODE − Click here for more information

Base class

Derived 1 Derived 2 Derived 3 Derived n

5) Hybrid Inheritance-
The inheritance which involves more than one inheritances is called as hybrid Inheritance.

Base

Derived 1 Derived 2

Derived

You might also like