Professional Documents
Culture Documents
A Paradigm Is A Standard, Perspective, or Set of Ideas. A Paradigm Is A Way of
A Paradigm Is A Standard, Perspective, or Set of Ideas. A Paradigm Is A Way of
Adding new data and function is not Adding new data and function is
easy. easy.
1) OOPs makes development and maintenance easier, whereas, in a procedure-oriented programming language,
it is not easy to manage if code grows as project size increases.
2) OOPs provides data hiding, whereas, in a procedure-oriented programming language, global data can be
accessed from anywhere.
OOPs Characteristics:
Object: Any entity that has state and behavior is known as an object. For example, a chair, pen, table,
keyboard, bike, etc. It can be physical or logical. An Object can be defined as an instance of a class. An object
contains an address and takes up some space in memory. Objects can communicate without knowing the details
of each other's data or code. The only necessary thing is the type of message accepted and the type of response
returned by the objects. Example: A dog is an object because it has states like color, name, breed, etc. as well as
behaviors like wagging the tail, barking, eating, etc.
Class: Collection of objects is called class. It is a logical entity. A class can also be defined as a blueprint from
which you can create an individual object. Class doesn't consume any space.
Encapsulation Binding (or wrapping) code and data together into a single unit are known as encapsulation. For
example, a capsule, it is wrapped with different medicines. A C++ class is the example of encapsulation. C++
bean is the fully encapsulated class because all the data members are private here.
Abstraction Hiding internal details and showing functionality is known as abstraction. For example phone call,
we don't know the internal processing.In C++, we use abstract class and interface to achieve abstraction.
Inheritance When one object acquires all the properties and behaviors of a parent object, it is known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism: If one task is performed in different ways, it is known as polymorphism. For example: to
convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.In C++, we
use method overloading and method overriding to achieve polymorphism. Another example can be to speak
something; for example, a cat speaks meow, dog barks woof, etc.
Character Set: In C++, character set is a set of all valid characters that can be used in a C++ Program.
Characters set is used to specify the characters or symbols recognized by the language. Character set is a set of
all valid characters that can be used to form words, numbers and expression's in source programs.
There are mainly four categories of the character set as shown in the Figure.
1. Alphabets Alphabets are represented by A-Z or a-z. C- Language is case sensitive so it takes different
meaning for small and upper case letters. By using this character set C statements and character constants can
be written very easily. There are total 26 letters used in C-programming.
2. Digits Digits are represented by 0-9 or by combination of these digits. By using the digits numeric constant
can be written easily. Also numeric data can be assigned to the C-tokens. There are total 10 digits used in the
C-programming.
3. Special Symbols All the keyboard keys except alphabet, digits and white spaces are the special symbols.
These are some punctuation marks and some special symbols used for special purpose. There are total 30
special symbols used in the C-programming. Special symbols are used for C-statements like to create an
arithmetic statement +, -, * etc. , to create relational statement <, >, <=, >=, == etc. , to create assignment
statement =, to create logical statement &&, II etc. are required.
4. White Spaces White spaces has blank space, new line return, Horizontal tab space, carriage ctrl, Form feed
etc. are all used for special purpose. Also note that Turbo-C Compiler always ignore these white space
characters in both high level and low level programming.
Tokens: A token is the smallest element of a C++ program that is meaningful to the compiler. The C++ parser
recognizes these kinds of tokens: identifiers, keywords, literals/constants, operators, punctuators and
separators. A stream of these tokens makes up a translation unit.
Identifiers: "Identifiers" or "symbols" are the names you supply for variables, types, functions, and labels in
your program. Identifier names must differ in spelling and case from any keywords. You cannot use keywords
as identifiers; they are reserved for special use. They must not start with digit. They can contain only digits,
alphabets and underscore.
Literals/constants: Constants refer to fixed values that the program may not alter during its execution. These
fixed values are also called literals. Constants can be of any of the basic data types like an integer constant, a
floating constant, a character constant, or a string literal.
Operators: An operator is a symbol that operates on a value or a variable. For example: + is an operator to
perform addition.
Arithmetic Operators An arithmetic operator performs mathematical operations such as addition,
subtraction, multiplication, division etc on numerical values (constants and variables). examples +,-,*,/,
%
Increment and Decrement Operators C programming has two operators increment ++ and
decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are
unary operators, meaning they only operate on a single operand.
Assignment Operators An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =
Relational Operators A relational operator checks the relationship between two operands. If the
relation is true, it returns 1; if the relation is false, it returns value 0.
Logical Operators An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used in decision making.
Punctuators and separators The following characters are used as punctuators (also known as separators) in
C++ :
[ ] ( ) { } , ; : * ... = #
Data Types: In C++, data types are declarations for variables. This determines the type and size of data and
associated operations of handling it. For example,
int age = 13;
C++ Variables
In programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). For example,
int age = 14;
Strings:
String is a collection of characters. “abc”
7 integer
‘7’character
“7”string
}
Recursion A function that calls itself is known as a recursive function. And, this technique is known as
recursion.
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main()
{
... .. ...
recurse();
... .. ...
}
To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the
recursive call and the other doesn't.
int factorial(int n)
{
if (n > 1)
return n * factorial(n - 1);
else
return 1;
}
Array
an array is a variable that can store multiple values of the same type. For example,
Suppose a class has 27 students, and we need to store the grades of all of them. Instead of creating 27 separate
variables, we can simply create an array:
double grade[27];
Here, grade is an array that can hold a maximum of 27 elements of double type.
In C++, the size and type of arrays cannot be changed after its declaration.
In C++, each element in an array is associated with a number. The number is known as an array index. We can
access elements of an array by using those indices.
// syntax to access array elements
array[index];
grade[1] etc.
The array indices start with 0. Meaning x[0] is the first element stored at index 0.
If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the last element.
Elements of an array have consecutive addresses. For example, suppose the starting address of x[0] is 2120d.
Then, the address of the next element x[1] will be 2124d, the address of x[2] will be 2128d and so on.
Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.
it's possible to initialize an array during declaration. For example,
Pointer: A pointer is a variable whose value is the address of another variable. Like any variable or constant,
you must declare a pointer before you can work with it. The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the pointer
variable. The asterisk you used to declare a pointer is the same asterisk that you use for multiplication.
However, in this statement the asterisk is being used to designate a variable as a pointer. Following are the valid
pointer declaration −
The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a
long hexadecimal number that represents a memory address. The only difference between pointers of different
data types is the data type of the variable or constant that the pointer points to.
#include <iostream>
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable
return 0;
}
When the above code is compiled and executed, it produces result something as follows −
Title
Author
Subject
Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more
than one member, for your program. The format of the struct statement is this −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The member access operator is
coded as a period between the structure variable name and the structure member that we wish to access. You
would use struct keyword to define variables of structure type. Following is the example to explain usage of
structure −
#include <iostream>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
When the above code is compiled and executed, it produces the following result −