0% found this document useful (0 votes)
9 views16 pages

Lec 08

The document provides an overview of C++ programming basics, focusing on variable types, input using 'cin', comments, constants, and library functions. It includes code examples demonstrating how to take user input, define constants, and use library functions for mathematical computations. Additionally, it explains the purpose of comments in code for better understanding and documentation.

Uploaded by

hijabmemon03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

Lec 08

The document provides an overview of C++ programming basics, focusing on variable types, input using 'cin', comments, constants, and library functions. It includes code examples demonstrating how to take user input, define constants, and use library functions for mathematical computations. Additionally, it explains the purpose of comments in code for better understanding and documentation.

Uploaded by

hijabmemon03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Object Oriented Programming:

C++

C++ Programming Basics


Variable Types Summary
Input With cin : Example

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int num1, num2;
cout<< “Enter the value of num1”;
cin >> num1;
cout<< “Enter the value of num2”;
cin>>num2;
cout << “the sum of num1 and num2 is” << num1 + num2;
getch();
}
Input Using cin
– cin is predefined object that is used to take data from the
keyboard. cin is predefined in C++ library.
– The operator >> is called the extraction or get-from operator. It
takes the value from the stream object (cin) on its left and
places it in the variable on its right.
Comments
• A comment is the portion of a program that exists only for
the human reader
• Comments are ignored by the compiler
• They are generally used for short explanations or notes
relevant to the code.
• They help the person writing a program, and anyone else
who must read the source file, understand what’s going on.
• Comments can be used to document code
• Types of comments in C++.
– Single line comments begin with // anywhere in the line
– Multiple line comments are enclosed between /* and */
Input With cin : Example

#include <iostream>
#include <conio.h>
using namespace std;
/* This program takes two integer numbers from user through cin statement and
print their sum on the screen. */
int main()
{
int num1, num2;
cout<< “Enter the value of num1”;
cin >> num1;
cout<< “Enter the value of num2”;
cin>>num2;
//prints the sum of the numbers on the screen
cout << “the sum of num1 and num2 is” << num1 + num2;
getch();
}
Escape Sequences
• There are many escape sequences in C++, which allow you to
control the output.
• The name reflects the fact that the backslash causes an
“escape” from the normal way characters are interpreted.
Constant
• Constant cannot be change by program.
• Constant has fixed value.
• Constant cannot be change the value at Run
Time.
• const float PI=3.14; is a constant with a fixed
value and may not be change by the the
program as the program runs.
Defining Constants
There are two simple ways in C++ to define constants:
• Using #define preprocessor.
• Using const keyword.

The #define Preprocessor:


Following is the form to use #define preprocessor to define a
constant:
• #define identifier value
Defining Constants
• Following example explains it in detail:
#include <iostream>
#include<conio.h>
using namespace std;
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main()
{
int area;
area = LENGTH * WIDTH;
cout << area; cout << NEWLINE;
Getch();}
When the above code is compiled and executed, it produces following
result:50
Defining Constants
The const Keyword:
• You can use const prefix to declare constants
with a specific type as follows:
• const type variable = value;
Defining Constants
• Following example explains it in detail:
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{ const int LENGTH = 10;
const int WIDTH = 5;
const char NEWLINE = '\n';
int area; area = LENGTH * WIDTH;
cout << area; cout << NEWLINE;
getch();
}
When the above code is compiled and executed, it produces following
result:50
Library Functions
– Library functions are predefined functions in C++; e.g.,
sqrt(num), pow (num, raised_to_num)
– Library functions are used to perform:
• File access
• Mathematical computations
• Data conversions, and so on
Library Functions
To find the power of the given number:

#include<iostream>
#include<conio.h>
#include<cmath>
using namespace std;
int main()
{
double number,answer, num1;
cout<<"Enter number ";
cin>>number;
cout<<"Enter power of the number";
cin>>num1;
answer=pow(number,num1);
cout<<"The power of the number is "<<answer;
getch();
}
Library Functions
Library Functions

You might also like