CPP Module-1 Notes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

OBJECT

ORIENTED
PROGRAMMING
USING C++
1. Features of C++ ?

• Object-Oriented Language.
@RESTRICTED • High Level & English-like Language.

MODULE-1 • Easy to Learn.

• Easy to extend functionality.


• Fundamentals of Programming
• Commonly used for making OS & Games.
• Basic C++ Concepts
• Call of Duty,PUBG,Tomb Raider are examples.

• C++ Added OOP concept to existing ‘C’

• Easy to debug.

• Can create complex programs easily.

2. Define Data type ?

Type of data that a variable holds is datatype

eg: int, float, double, char


3. Fundamental(Primitive) Data types of C++ ?

Boolean Datatype ( bool )

For storing boolean types 0,1(true or false)

Eg: bool X = “true” ; OR bool X = 0 ;

Integer Datatype ( int )

For storing Integers like 0, 10 , -8 etc.

Character Datatype ( char )

For storing a single ASCII character like ‘A’

Decimal Datatype ( float/double)

For storing Values which have decimal value like 3.1415

4. List Some Preprocessor directives & Usage ?

• #if and #endif - To check conditions

• #include - To include a header file

• # pragma - To include Macros


Fig of Q.5 5. Explain an Array & As Homogenous Aggregate ?

• Collection of Same datatype located at next-to-


Position
0 1 2 3 next memory location is called an ARRAY.
Values 8 56 12 24 • Index is the starting point of an array( 0 ).

• Array maybe One-Dimensional or 2-D


This is the Correct form
Array as Homogenous

Consider the figure on left.We can say Array is


Homogenous because it contains only same
Position
0 1 2 3
datatype. So, Array is a Homogenous Aggregate.
Values 8 5.6 ‘A’ 1.21
{NOTE: Draw the figures on Exams for Full Score}

6. Define Structure ?
This is the Wrong form

Collection of different datatypes under a


common name is structure.

Syntax struct structure name{


Datatype Variable;

}
Fig of Q.7 7. Explain Structure as Heterogenous Aggregate ?

• Collection of different datatypes under a


common name is called a Structure.

• Can include both same or different datatype

• Dot operator( . ) is used to access members.


8 ‘A’

Integer Character Structure as Homogenous


3.14

Float Consider the figure on left.We can say Structure


as Heterogenous because it contains any type of
Inside a Structure
datatype. So,Structure is a Heterogenous
Aggregate.

{NOTE: Draw the figures on Exams for Full Score}

8. List some basic Storage Classes ?

• Automatic Storage class ( auto )

• Register Storage class ( register )

• Static Storage class( static ) , External ( extern )


Fig of Q.9 9. Explain Difference between Union & Structure ?

• Collection of different datatypes under a


common name is called a Structure.Structure is
declared using keyword ‘ struct ‘.
34 34900.50
• Union is same as structure but only the size of
Age Salary
largest datatype is used to allocate
memory.Union is declared using ‘ union ’.

Structure Employee After Execution Example

struct Employee{ union Employee{

int Age; int Age;


Next Value(Salary) Overwrites
double Salary; double Salary;
Current Value(Age)

Double Datatype }; };

Union Employee After Execution


{NOTE: Figures Only for Understanding🤪}
10. Difference btw Exit & Entry Controlled Loops ?

Entry Controlled Exit Controlled

Only Executes when Condition is Executes once even condition is


true. false.
Condition is checked on Condition is checked after
entering loop entering loop
For Loop & While Loops are
Do-While Loop is an Example
Examples

11. Why is Main Function Important in C++ ?

The Program will only starts executing inside the


main function. If There is No main function, the
Compiler will generate errors & warnings

12. What is setw( ) function ?

setw( ) is a function inside the header file


iomanip.h

it’s sets the width of the terminal output.


13. Explain For Loops in C++ ?
For Example
Statements used to repeat a particular task is
known as loops.

for(count=4; count>=0; count-- ) For Loop


{ For Loop is an Entry Controlled Loop where
cout<<“Hello\n”; initialization, updation & conditions are checked
} in a Single Statement.

————————-OUTPUT——————————— for(initialization; condition; updation)


Hello {

Hello //Code to Repeat

Hello }
Hello
14. What is Break Statement ?
Hello
Break is used to Exit a loop or switch

Syntax break ;
15. Explain While Loops in C++ ?
While Example
While loop is an Entry Controlled loop where
condition is checked first & initialization is
int count = 4; declared above loop.The updation is done inside

while(count >= 0) the loop.

{ initialization;

cout<<“Hello\n”; while( condition )


count--;
{
}
//Code to Repeat

updation;
————————-OUTPUT———————————
}
Hello

Hello 16. What is Continue Statement ?

Hello Continue is used to skip a particular statement.


Hello Syntax continue ;
Hello
17. Explain Do-While Loops in C++ ?
Do-while Example Do while loop is an exit controlled loop where
condition is checked after exiting loop.So it’ll
int count = 4; execute once even condition is false.

do{ initialization;
cout<<“Hello\n”;
do
count--;
{
}while(count >= 0);
//Code to Repeat

updation;
————————-OUTPUT———————————

Hello } while( condition ) ;

Hello

Hello

Hello

Hello
19. Explain Switch Statements in C++ ?
Switch Sample
Switch is a used to check conditions in program.

switch( Variable ) {

case Value : //code to execute


int day;
break;
switch(day){
case Value N : //code to execute
case 1: cout<<“Sunday”;
break;
break;

case 2: cout<<“Monday”; default : //default code

break; };

.. .. .. .. ..

case 7: cout<<“Saturday”;

break;
‘;’ is not important but it’s recommended
default: cout<<“Wrong!”; just to beautify the code😘😍

};
19. Explain If Statements in C++ ?
IF Sample
If statements are used to check a condition in
int day;
program.
if(day == 1){
IF ELSE
cout<<“Sunday”; Simple IF IF ELSE
LADDER
} To check a single To Check more than 1 For checking multiple
Condition condition conditions.
else if(day == 2){
if(condition) if(condition) if(condition)
cout<<“Monday”; { { {
True Block True Block-1
} Statements
} }
}
else else if(con)
.. .. .. .. .. .. ..
{
{
else if(day == 7){ True Block-2
False Block
}
cout<<“Saturday”; }
.. .. .. ..
else if(con)
}
{
else{ True Block-n
}
cout<<“Wrong Number!”; else
{
} False Block
}
20. Explain Derived datatypes in C++ ?

Datatypes that are created from an existing


datatype(Fundamental Datatype) is called
Derived Datatype.

Eg: Arrays,Structure,Class.

21. Explain Jump Statements in C++ ?

Jump statements are used to transfer program


control to another part

Explain These that I’ve given above

• Continue Statement

• Break Statement

22. Explain Iteration Statements in C++ ?

Explain These that I’ve given above

• For , While , Do-While Loops


23. Explain Control Statements in C++ ?
QUESTIONS FROM 24 NOT
COMPULSORY TO PASS THE Explain These that I’ve given above

EXAM • IF Statements
ONLY TO GUARANTEE 100!
• Switch Statements

24. Explain I/O Functions in C++ ?

cout

• Stream Insertion Operator ( << ) is used to


display Result

• cout<<Value1<<Value2<<….<<ValueN;

cin

• Stream Extraction Operator( >> ) is used to


read input

• cin>>Value1>>Value2>>….>>ValueN;

ALL THESE FUNCTIONS ARE IN “iostream” header.


25. Explain File I/O in C++ ?
THIS SECTION IS NOT
COMPULSORY TO PASS THE HEADER FILE

EXAM The header file “fstream” has many classes &


ONLY TO GUARANTEE 100! functions that handles file input & outputs.

STANDARD FILE CLASSES

• ofstream - output file stream

• Ifstream - input file stream

EXAMPLE

ofstream newFile;

newFile.open(“location. extension”);

newFile<<“Message to write “;

newFile.close( );
26. Explain Common Error Streams in C++ ?
THIS SECTION IS NOT
COMPULSORY TO PASS THE clog

EXAM • To print Warnings


ONLY TO GUARANTEE 100!
• Stream Insertion (<<) operator is used.

cerr

• To print Error Messages.

• Stream Insertion(<<) operator is used

EXAMPLE

clog<<“Hi! This is a Warning!”;

cerr<<“OOPS! This is a Error!”;

27. What is use of namespace std ?

iostream has lots of objects named cout, cin.

So, it’s like a initial so that no conflicts occur!

You might also like