Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

BCA IInd Year : Unit 1 -OOPs:

Programming paradigm: A paradigm is a standard, perspective, or set of ideas. A paradigm is a way of


looking at something. The word paradigm comes up a lot in the academic, scientific, and business worlds. ...
When you change paradigms, you're changing how you think about something.

Procedural Programming: Procedural Programming can be defined as a programming model which is derived


from structured programming, based upon the concept of calling procedure. Procedures, also known as routines,
subroutines or functions, simply consist of a series of computational steps to be carried out.
Languages used in Procedural Programming: FORTRAN, ALGOL, COBOL,
BASIC, Pascal and C.
Object Oriented Paradigms: Object-oriented programming (OOP) is a programming paradigm based
upon objects (having both data and methods) that aims to incorporate the advantages of modularity and
reusability. Objects, which are usually instances of classes, are used to interact with one another to design
applications and computer programs.
Languages used in Object Oriented Programming:C++, C++, C#, Python, PHP, C++Script, Ruby, Perl,
Objective-C, Dart, Swift, Scala.
Difference between Procedural and Object Oriented Programming:

In procedural programming, program is In object oriented programming,


divided into small parts program is divided into small parts
called functions. called objects.

Procedural programming follows top Object oriented programming


down approach. follows bottom up approach.

Object oriented programming have


There is no access specifier in access specifiers like private, public,
procedural programming. protected etc.

Adding new data and function is not Adding new data and function is
easy. easy.

Procedural programming does not have Object oriented programming


any proper way for hiding data so it provides data hiding so it is more
is less secure. secure.

In procedural programming, Overloading is possible in object


overloading is not possible. oriented programming.
In procedural programming, function is In object oriented programming, data
more important than data. is more important than function.

Procedural programming is based Object oriented programming is


on unreal world. based on real world.

Examples: C, FORTRAN, Pascal, Examples: C++, C++, Python, C#


Basic etc. etc.

Advantage of OOPs over Procedure-oriented programming language

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.

Figure: Data Representation in Procedure-Oriented Programming

Figure: Data Representation in Object-Oriented Programming


3) OOPs provides the ability to simulate real-world event much more effectively. We can provide the solution
of real word problem if we are using the Object-Oriented Programming language.

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.

C++Introduction: C++ is a cross-platform language that can be used to create high-performance


applications. C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives
programmers a high level of control over system resources and memory.
Learning English:
Letters-->Words-->Sentences-->Paragraphs/Essays
Learning C++
Character Set-->Token-->Statements-->Program/Project/Software

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.

Keywords: Keywords are those words whose meaning is already defined by Compiler.


Auto Else long switch

Break Enum register typedef

Case Extern return union

Char Float short unsigned

Const For signed void

Continue Goto sizeof volatile

Default If static while

Do Int struct _Packed

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++ :
[ ] ( ) { } , ; : * ... = #

Basic Program in C++:


#include<iostream.h>
void main()
{
cout<<"hello";
}
Program to find sum of two numbers:
#include<iostream.h>
void main()
{
int a,b,sum;
cin>>a>>b;
sum=a+b;
cout<<sum;
}
Shortcut Keys:
Alt + F9 Compile
Ctrl + F Run
Alt + F5 To see output

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;

Fundamental Data Types: int, float, char, double


Derived Data Type: Array, pointer
User Defined: Structure, Class, Function, enum, union

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

Functions: A function is a block of code that performs a specific task.


Suppose we need to create a program to create a circle and color it. We can create two functions to solve this
problem:
 a function to draw the circle
 a function to color the circle
Dividing a complex problem into smaller chunks makes our program easy to understand and reusable.
There are two types of function:
 Standard Library Functions: Predefined in C++
 User-defined Function: Created by users
C++ User-defined Function
C++ allows the 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 the program, it all executes the codes defined in the body of the
function.
C++ Function Declaration
The syntax to declare a function is:
returnType functionName (parameter1, parameter2,...) {
// function body
}
Here's an example of a function declaration.
 
// function declaration
void greet() {
cout << "Hello World";
}
Here,
the name of the function is greet()
the return type of the function is void
the empty parentheses mean it doesn't have any parameters
the function body is written inside {}
Note: We will learn about returnType and parameters later.
Calling a Function
In the above program, we have declared a function named greet(). To use the greet() function, we need to call it.
Here's how we can call the above greet() function.
int main() {
// calling a function
greet();

}
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();
... .. ...
}

The recursion continues until some condition is met.

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,

// declare and initialize and array


int x[6] = {19, 10, 8, 17, 9, 15};

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 −

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

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.

Using Pointers in C++


There are few important operations, which we will do with the pointers very frequently. (a) We define a pointer
variable. (b) Assign the address of a variable to a pointer. (c) Finally access the value at the address available in
the pointer variable. This is done by using unary operator * that returns the value of the variable located at the
address specified by its operand. Following example makes use of these operations −

#include <iostream>
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable

ip = &var; // store address of var in pointer variable


cout << "Value of var variable: ";
cout << var << endl;

// print the address stored in ip pointer variable


cout << "Address stored in ip variable: ";
cout << ip << endl;

// access the value at the address available in pointer


cout << "Value of *ip variable: ";
cout << *ip << endl;

return 0;
}

When the above code is compiled and executed, it produces result something as follows −

Value of var variable: 20


Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20
Structures:
Structure is another user defined data type which allows you to combine data items of different kinds.
Structures are used to represent a record, suppose you want to keep track of your books in a library. You might
want to track the following attributes about each book −

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 [structure tag] {


member definition;
member definition;
...
member definition;
} [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f;
or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can
specify one or more structure variables but it is optional. Here is the way you would declare the Book structure

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;

// Print Book1 info


cout << "Book 1 title : " << Book1.title <<endl;
cout << "Book 1 author : " << Book1.author <<endl;
cout << "Book 1 subject : " << Book1.subject <<endl;
cout << "Book 1 id : " << Book1.book_id <<endl;

// Print Book2 info


cout << "Book 2 title : " << Book2.title <<endl;
cout << "Book 2 author : " << Book2.author <<endl;
cout << "Book 2 subject : " << Book2.subject <<endl;
cout << "Book 2 id : " << Book2.book_id <<endl;

return 0;
}
When the above code is compiled and executed, it produces the following result −

Book 1 title : Learn C++ Programming


Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700

You might also like