OOSD Unit-4
OOSD Unit-4
BATCH 2023-2024
OVERVIEW OF C LANGUAGE:
1.C language is known as structure oriented language or procedure oriented language
2.Employs top-down programming approach where a problem is viewed as a sequence of tasks to
be performed.
3.All program code of c can be executed in C++ but converse many not be possible
4. Function overloading and operator overloading are not possible.
5. Local variables can be declared only at the beginning of the block.
6. Program controls are through jumps and calls to subroutines.
7.Polymorphism, encapsulation and inheritance are not possible.
For solving the problems, the problem is divided into a number of modules. Each module is a
subprogram.
8. Data abstraction property is not supported by procedure oriented language.
9. Data in procedure oriented language is open and can be accessed by any function.
OVERVIEW OF C++ LANGUAGE:
1. C++ can be considered as an incremental version of c language which consists all programming language constructs with newly added
features of object oriented programming.
2. C++ is object oriented programming language.
3. The file extension of C++ program is “.CPP”
4. Function overloading and operator overloading are possible.
5. Variables can be declared in inline i.e. when required
6. In c++ more emphasis is give on data rather than procedures.
7. Polymorphism, encapsulation and inheritance are possible.
8. Data abstraction property is supported by c++.
9. Data access is limited. It can be accessed by providing various visibility modes both for data and member functions.
10. Dynamic binding is supported by C++
11. It supports all features of c language
12. It can be called as an incremental version of c language
DIFFERENCE BETWEEN PROCEDURE ORIENTED PROGRAMMING
(POP) & OBJECT ORIENTED PROGRAMMING (OOP)
DIFFERENCE BETWEEN PROCEDURE ORIENTED PROGRAMMING
(POP) & OBJECT ORIENTED PROGRAMMING (OOP)
BASIC STRUCTURE OF C++ LANGUAGE:
A C++ program should have one or more sections but the sequence of sections is to be
followed.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section & class declarations
5. Member function definition
6. Main function
1. DOCUMENTATION SECTION :
1. Document section comes first and is used to document the use of logic or reasons in
your program.
2. It can be used to write the program's objective, developer and logic details.
3. The documentation is done in C++ language with /* and */ . Whatever is written
between these two are called comments.
2. LINKING SECTION :
1. This section tells the compiler to link the certain occurrences of keywords or functions in
your program to the header files specified in this section.
2. e.g. #include<iostream>
using namespace std;
3. Directive causes the preprocessor to add the contents of the iostream file to the program. It
contains declarations for cout and cin.
4. cout is a predefined object that represents the standard output stream. The operator << is an
insertion operator, causes the string in double quotes to be displayed on the screen.
5. The identifier cin is a predefined object in C++ that corresponds to the standard input stream.
The operator >> is known as extraction operator. It extracts the value from the keyboard and
assigns it to the value variable on its right.
3. DEFINITION SECTION :
1. It is used to declare some constants and assign them some value. e.g. #define MAX 25
2. Here #define is a compiler directive which tells the compiler whenever MAX is found
in the program replace it with 25.
4. GLOBAL DECLARATION SECTION :
1. Here the variables and class definations which are used through out the program
(including main and other functions) are declared so as to make them global(i.e
accessible to all parts of program).
2. A CLASS is a collection of data and functions that act or manipulate
the data. The data components of a class are called data members and function
components of a class are called member functions.
3. A class can also termed as a blue print or prototype that defines the variable or
functions common to all objects of certain kind.
4. It is a user defined data type.
5. e.g. int i; //this declaration is done outside and before main()
5. SUB PROGRAM OR FUNCTION SECTION :
1. ex:
#include<iostream>
using namespace std;
namespace sample
{
int m;
void display(int n)
{
cout<<"in namespace N="<<n<<endl;
}
}
IDENTIFIERS:
1. Identifiers are the names given to various program elements such as variables, functions and
arrays.
2. These are user defined names consisting of sequence of letters and digits.
3. Rules for declaring identifiers:
The first character must be an alphabet or underscore.
It must consist of only letters, digits and underscore.
Identifiers may have any length but only first 31 characters are significant.
It must not contain white space or blank space.
We should not use keywords as identifiers.
Upper and lower case letters are different.
IDENTIFIERS:
1. The declaration of variable gives the name for memory location and its size.
2. It specifies the range of value that can be stored in that location.
3. Syntax:
Data_type variable_name;
4. Ex:
int a=10;
float x=2.3;
CONSTANTS:
1. Constants refer to fixed values that the program may not alter and they are called literals.
2. Constants can be of any of the basic data types and can be divided into Integer Numerals,
Floating-Point Numerals, Characters, Strings and Boolean Values.
3. Again, constants are treated just like regular variables except that their values cannot be
modified after their definition.
4. Integer Literals: An integer literal can be a decimal, octal, or hexadecimal constant. A prefix
specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
85 // decimal
0213 // octal
0x4b // hexadecimal
ENUM:
1. // Defining enumYear
2. enum year { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
3. int main()
4. {
5. int i;
6. // Traversing the year enum
7. for (i = Jan; i <= Dec; i++)
8. cout << i << " ";
9. return 0;
10. }
11. Output:
12. 0 1 2 3 4 5 6 7 8 9 10 11
OPERATORS:
sizeof
1 sizeof operator returns the size of a variable. For example, sizeof(a), where ‘a’ is integer, and will return 4.
Condition ? X : Y
2 Conditional operator (?). If Condition is true then it returns value of X otherwise returns value of Y.
3 Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.
4 Member operators are used to reference individual members of classes, structures, and unions.
Cast
5 Casting operators convert one data type to another. For example, int(2.2000) would return 2.
&
6 Pointer operator & returns the address of a variable. For example &a; will give actual address of the variable.
7 Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var.
PRECEDENCE OF OPERATORS :
TYPECASTING:
1. Conversion using Cast operator: A Cast operator is an unary operator which forces one data type to be
converted into another data type.
2. int main()
3. {
4. float f = 3.5;
5. // using cast operator
6. int b = static_cast<int>(f);
7. cout << b;
8. }
9. Output:
10. 3
CONTROL STRUCTURES:
Control
Statement
while do for
CONTROL STRUCTURES:
If(test expression) Nesting of If—else Statement
{ If(test condition1)
Statement-block; {
} if(test condition2)
{
----------------------------------------------------------------
Statement-1;
If-else statement
}
if(test expression)
else
{ {
True-Block Statement(s); Statement-2;
} }
Else }
else
{
{
False-Block statement(s);
Statement-3;
}
}
CONTROL STRUCTURES:
CONTROL STRUCTURES:
Switch(expression)
Case value-1:
block-1;
break;
Case value-2:
block-2;
break;
……
……
default:
default-block;
break;
}
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
FUNCTION IN C++
MACRO
It is also called preprocessors directive. The macros are defined by the #define keyword. Before the
program compilation, the preprocessor examines the program whenever the preprocessor detects the macros
then preprocessor replaces the macro by the macro definition.
Syntax of Macro:
#define MACRO_NAME Macro_definition
Example of Macro:
#include <iostream>
using namespace std;
// macro with parameter
#define MAXIMUM(a, b) (a > b) ? a : b
// Main function for the program Output:
int main()
{
Max (100, 1000):1000 Max (20, 0):20
cout << "Max (100, 1000):";
int k = MAXIMUM(100, 1000);
cout << k << endl;
cout << "Max (20, 0):";
int k1 = MAXIMUM(20, 0);
cout << k1;
return 0;
}
DIFFERENCE BETWEEN INLINE FUNCTION AND MACRO
S.NO Inline Macro
1. An inline function is defined by the inline keyword. Whereas the macros are defined by the #define keyword.
2. Through inline function, the class’s data members can be accessed. Whereas macro can’t access the class’s data members.
3. In the case of inline function, the program can be easily debugged. Whereas in the case of macros, the program can’t be easily debugged.
Whereas in the case of macro, the arguments are evaluated every time
4. In the case of inline, the arguments are evaluated only once.
whenever macro is used in the program.
In C++, inline may be defined either inside the class or outside the Whereas the macro is all the time defined at the beginning of the
5.
class. program.
In C++, inside the class, the short length functions are automatically
6. While the macro is specifically defined.
made the inline functions.
7. Inline is not as widely used as macros. While the macro is widely used.
8. Inline is not used in competitive programming. While the macro is very much used in competitive programming.