preparation of CS201 only 1 hour
Important Topic + Quizzes + Subjective
Data
Types of C++
Mostly used
data Types are following
1)
Int (Integer whole number) For example: int num=2; it takes 4 bytes
&Stores whole numbers, without decimals
2)
Float (Floating point number) For example: float num=2.1; it takes 4 bytes Stores fractional
numbers, containing one or more decimals. Sufficient for storing 7 decimal
digits
3) Double (Floating point number) For example: float num=3.19; it takes 8 bytes
Stores fractional numbers, containing one or more decimals. Sufficient for
storing 15 decimal digits
4)
Char
(Character) For example: char letter= ’A’;
it takes 1 byte Stores a single
character/letter/number, or ASCII values
5) String (string) For Example: string
text= ”Hello”
What Variables and it’s scope:
A variable is a quantity that may change within the
passage of time is variable. (General definition )
OR
Variables are
containers for storing data values. In C++, there are different types of variables (defined
with different keywords), FOR example: int - stores integers
(whole numbers), without decimals, such as 123 or -123. double - stores
floating point numbers, with decimals, such as 19.99 or -19.99.
Arithmetic operators:
Arithmetic operators are used to perform common mathematical operations.
Addition (+) Adds
together two values/variables For
Example: X+Y
Subtraction(-) Subtracts one values/variables
from another For Example: X-Y
Multiplication (*)Multiplies Two values/variables For Example: X-Y
Division (/) Divides Two values/variables For Example: X/Y
Modulus
(%) Returns the division remainder For Example: X%Y
Increment(++)Increases
the value of a variable by 1 For
Example: ++x
Decrement (--)Decreases the value of
a variable by 1 For Example: --x
Conditional operators in C++:
- Less
than: a < b
- Less
than or equal to a <= b
- Greater
than: a > b
- Greater
than or equal to a >= b
- Equal
to a == b
- Not
Equal to: a != b
C++ decision making statements
An ‘if’
statement consists of a boolean expression followed by one or more statements.
An ‘if’
statement can be followed by an optional ‘else’ statement, which executes when
the boolean expression is false.
A
‘switch’ statement allows a variable to be tested for equality against a list
of values.
You can
use one ‘if’ or ‘else if’ statement inside another ‘if’ or ‘else if’
statement(s).
You can
use one ‘switch’ statement inside another ‘switch’ statement(s).
Loops In C++
Repeats a
statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.
Like a ‘while’
statement, except that it tests the condition at the end of the loop body.(Mostly repeated MCQS)
Execute a
sequence of statements multiple times and abbreviates the code that manages the
loop variable.
You can use one
or more loop inside any another ‘while’, ‘for’ or ‘do..while’ loop.
Functions:
Youtube reference link mustwatch(https://www.youtube.com/watch?v=ifSs1aDzi9M&t=189s)
A function is a group of statements that together perform a task.
Every C++ program has at least one function, which is main(), and
all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you
divide up your code among different functions is up to you, but logically the
division usually is such that each function performs a specific task.
A function declaration tells the compiler about a
function's name, return type, and parameters. A function definition provides
the actual body of the function.
The C++ standard library provides numerous built-in functions that
your program can call. For example, function strcat() to
concatenate two strings, function memcpy() to copy one memory
location to another location and many more functions.
A function is known with various names like a method or a
sub-routine or a procedure etc.
General syntax of
Function
return_type
function_name( parameter list ) {
body of the function
}
·
Return
Type − A function may
return a value. The return_type is the data type of the value
the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
·
Function
Name − This is the
actual name of the function. The function name and the parameter list together
constitute the function signature.
·
Parameters − A parameter is like a placeholder. When
a function is invoked, you pass a value to the parameter. This value is
referred to as an actual parameter or argument. The parameter list refers to the
type, order, and the number of the parameters of a function. Parameters are
optional; that is, a function may contain no parameters.
·
Function
Body − The function of the body contains a collection of statements that define what the function does.
Arrays In C++
YouTube Link for complete understanding Mustwatch (https://www.youtube.com/watch?v=_5_-UoyzXrk)
the array, which stores a fixed-size sequential collection of elements
of the same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of the same
type.
Syntax of array is type arrayName [ arraySize ];
C++ supports
multidimensional arrays. The simplest form of the multidimensional array is the
two-dimensional array.
You can generate
a pointer to the first element of an array by simply specifying the array name,
without any index.
You can pass to
the function a pointer to an array by specifying the array's name without an
index.
C++ allows a
function to return an array.
C++ Classes and Objects
YouTube reference Link Must Watch: (https://www.youtube.com/watch?v=zxiYjuLPQBE)
The main purpose
of C++ programming is to add object orientation to the C programming language
and classes are the central feature of C++ that supports object-oriented
programming and are often called user-defined types.
A class is used
to specify the form of an object and it combines data representation and
methods for manipulating that data into one neat package. The data and
functions within a class are called members of the class
What is class?
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.
Example:
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
C++
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
Two objects are
below in example:
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Very Very
important Below All the Topic Don’t miss it J
A member
function of a class is a function that has its definition or its prototype
within the class definition like any other variable.
A class member
can be defined as public, private or protected. By default members would be
assumed as private.
Constructor &
Destructor (Most Important)
A class
constructor is a special function in a class that is called when a new object
of the class is created. A destructor is also a special function which is
called when created object is deleted.
The copy
constructor is a constructor which creates an object by initializing it with an
object of the same class, which has been created previously.
A friend function
is permitted full access to private and protected members of a class.
With an inline
function, the compiler tries to expand the code in the body of the function in
place of a call to the function.
Every object has
a special pointer this which points to the object itself.
A pointer to a
class is done exactly the same way a pointer to a structure is. In fact, a class
is really just a structure with functions in it.
Both data
members and function members of a class can be declared as static.
Templates
Youtube Video Link (https://www.youtube.com/watch?v=xNBWR_uFqQ8)
General syntax:
template <class type> ret-type func-name(parameter list) {
// body of function