Unit 1
Unit 1
int main() {
cout << "Hello World!";
return 0;
}
Omitting Namespace
• #include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Addition program
• #include <iostream>
• using namespace std;
• int main() {
• int a=8,b=7,c;
• c=a+b;
• cout << "sum="<<c;
• return 0;
• }
Variables
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Control Structures
• if Statement
• if (condition) {
// block of code to be executed if the condition is true
}
• if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
else if Statement
• if (condition1) {
// block of code to be executed if condition1 is
true
} else if (condition2) {
// block of code to be executed if the condition1 is
false and condition2 is true
} else {
// block of code to be executed if the condition1 is
false and condition2 is false
}
Greatest of two numbers
• #include <iostream>
• using namespace std;
• int main() {
• int num1, num2;
• cout << "Enter two numbers: ";
• cin >> num1 >> num2;
• if (num1 > num2) {
• cout<<num1<<" is greater";
• }
• else
• {
• cout<<num2<<"is greater";
• }
• return 0;
• }
Greatest of three numbers
• #include <iostream>
• using namespace std;
• int main() {
• int num1, num2, num3;
• cout << "Enter three numbers: ";
• cin >> num1 >> num2 >> num3;
• if ((num1 > num2) && (num1>num3))
• {
• cout<<num1<<" is greater";
• }
• else if ((num2> num1) && (num2>num3))
• {
• cout<<num2<<"is greater";
• }
• else
• cout<<num3<<"is greater";
• return 0;
• }
loops
• Encapsulation is defined as binding together the data and the functions that
manipulate them.
• Consider a real-life example of encapsulation, in a company, there are different
sections like the accounts section, finance section, sales section, etc. The
finance section handles all the financial transactions and keeps records of all
the data related to finance. Similarly, the sales section handles all the sales-
related activities and keeps records of all the sales. Now there may arise a
situation when for some reason an official from the finance section needs all the
data about sales in a particular month. In this case, he is not allowed to directly
access the data of the sales section. He will first have to contact some other
officer in the sales section and then request him to give the particular data. This
is what encapsulation is.
Abstraction