0% found this document useful (0 votes)
8 views

L2_Intro_to_OOP

The document provides an introduction to the basic structure of C++ programming, including sections for directives, the main function, and function bodies. It covers basic input/output operations using streams like cout and cin, and includes example programs for displaying messages and performing calculations. Additionally, it features multiple-choice questions to test understanding of C++ concepts.

Uploaded by

parishree1906
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

L2_Intro_to_OOP

The document provides an introduction to the basic structure of C++ programming, including sections for directives, the main function, and function bodies. It covers basic input/output operations using streams like cout and cin, and includes example programs for displaying messages and performing calculations. Additionally, it features multiple-choice questions to test understanding of C++ concepts.

Uploaded by

parishree1906
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

SOFTWARE DEVELOPMENT FUNDAMENTALS-2

Introduction to object oriented programming

Lecture 2
Basic structure of C++
▪ C++ program has following sections in its basic structure,
▪ Directives/libraries inclusion section
▪ Main function section
▪ Function body section

▪ Directives inclusion section allows to include the required libraries for the
program.
▪ Main function section defines the sequence of code that has to be executed.
▪ Function body section defines the functionality of the functions. It has
declaration and executable statements.
Basic structure of C++
#include <directives> Directives inclusion section

using namespace std;


int main()
{
Declaration statements Main function section
Function body section
Executable statements
return 0;

}
Basic structure of C++
#include <directives> It includes the directives/libraries mentioned in angle brackets

using namespace std;

int main()

{
Declaration statements
Executable statements
return 0;

}
Basic structure of C++
#include <directives>
This statement informs compiler to use std namespace in which the
using namespace std;
standard C++ libraries are declared.
int main()

{
Declaration statements
Executable statements
return 0;

}
Basic structure of C++
#include <directives>

using namespace std;

int main()
Main function definition part.
{
Declaration statements
Executable statements
return 0;

}
Basic Input/Output in C++
The following are operations used for input and output in C++,

S. No. Operat Purpose


ion
1 cout Standard Output stream
2 cin Standard Input stream
3 cerror Standard error output stream
4 clog Standard log output stream
Basic Input/Output in C++
▪ cout basically outputs stream on console.

▪ cerror is used to output error messages on standard console.

▪ clog directs the log messages to standard console output.

▪ cerror and clog can be redirected to another output stream.


Output in C Output in C++
printf(“%d”, x); cout<<x;
printf(“Hello World”); cout<<“Hello World”;
printf(“The average is %f”, avg); cout<<“The average is ”<<avg;
printf(“\n”); cout<<endl;
Basic Input/Output in C++
▪ cin basically inputs stream and directs to the variable.

Input in C Input in C++


scanf(“%d”, &x); cin>>x;
scanf(“%s”, str); cin>>str;
scanf(“%d %s”, &num, str); cin>>num>>str;
A program in C++ to display “
Hello World”
#include <iostream>
using namespace std;

int main()
{
cout << "Hello World";

return 0;
}
C++ program to add two
numbers
#include <iostream>
using namespace std;

int main() {

int first_number, second_number, sum;

cout << "Enter two integers: ";


cin >> first_number >> second_number;
sum = first_number + second_number;

cout << first_number << " + " << second_number << " = " << sum;

return 0;
}
#include <iostream>
using namespace std;
int main() {

double n1, n2, n3;


cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


C++ program to find cout << "Largest number: " << n1;

largest of three numbers else if(n2 >= n1 && n2 >= n3)


cout << "Largest number: " << n2;

else
cout << "Largest number: " << n3;

return 0;
}
WAP in C++ to find the sum of its elements in
an array

#include <iostream.h>
using namespace std
int sum(int arr[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int main()
{
int arr[] = { 12, 3, 4, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("Sum of given array is %d", sum(arr, n));
return 0;
}
Basic C++ MCQs
1. Which of the following user-defined
header file extension used in c++? Answer1 : C

a) hg
b) cpp
c) h
d) hf

2. Which of the following is used for comments Answer 2: d


in C++?
a) /* comment */
b) // comment */
c) // comment
d) both // comment or /* comment */
Basic C++ MCQs
3. Who invented C++?
a) Dennis Ritchie Answer : d
b) Ken Thompson
c) Brian Kernighan
d) Bjarne Stroustrup

4. What is output of below program?


#include<iostream>
int main()
{ (A) 10 Answer: C
const int a=10; (B) 11
a++; (C) Compilation Error
std::cout<<a; (D) Linking Error
return 0;
}
Basic C++ MCQs
#include<iostream>

void Execute(int &x, int y = 200)


{ 5. What is the ouput of the program
int TEMP = x + y; (A) 5020--5020--
x+= TEMP; (B) 5020--7012020--12020--
if(y!=200) (C) 5020--70120200--5020
std::cout<<TEMP<<x<<y<<"--"; (D) 5020--7050200--5020--
}
int main()
{ int A=50, B=20;
std::cout<<A<<B<<"--";
Answer : b
Execute(A,B);
std::cout<<A<<B<<"--";
return 0;
}
Basic C++ MCQs
#include<iostream>
int main() 6. What is the output of the program:
{ (A) Error
int i=0;
(B) 5 times
lbl:
std::cout<<"CppBuzz.com"; (C) 4 times
i++; (D) 6 times
if(i<5)
{
goto lbl;
} Answer: b

return 0;
}
References
▪Herbert Schildt, “C++: The complete reference”, Mc Graw Hill Osborne media, 4 th
edition, 2017
▪Robert Lafore, “Object oriented programming in C++”, SAMS, 4 th edition, 2002

You might also like