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

Lecture 1

C++ is an object-oriented programming language that is a superset of C and was developed in the early 1980s, the document discusses various C++ concepts like data types, operators, functions, and input/output and provides examples of defining variables and complex number operations in C++. It also describes creating header files to define type aliases and constants for complex numbers to make the code more readable and maintainable.

Uploaded by

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

Lecture 1

C++ is an object-oriented programming language that is a superset of C and was developed in the early 1980s, the document discusses various C++ concepts like data types, operators, functions, and input/output and provides examples of defining variables and complex number operations in C++. It also describes creating header files to define type aliases and constants for complex numbers to make the code more readable and maintainable.

Uploaded by

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

C++ for Mathematics

Sandeep Kumar
Introduction:
● C++ is a object oriented programming language (a superset of C).
● C++ = C + objects
● C++ was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill,
New Jersey, USA, in the early of the 1980 .
● Interpreted Languages: Python, Perl, BASIC, Mathematica code
● Compiled Languages : C, C++, Java, FORTRAN ...

Code is first processed in a form which can be understood by the machine. Complied
first translates the source file into object file. The loader (linker) further combines
object files into an executable file which can be run.

File Name
● The source file name first.cpp has two parts. Name of the file and its extension.
All C++ source file are saved with the extension .cpp.
● The source file can be compiled with the command g++, which invokes the
C++ compiler and generates the object file.
● By default the object file is given the name a.out.
● To run the object file the command is : ./a.out
● A different name to the object file can be given by the command

g++ first.cpp -o first.out

Now the object file is saved as first.out, which can be run with ./first.out
Introduction:
● #inlcude<iostream> commands includes the iostream library in the
source file, that includes standard basis C++ inbuilt input/output and
other functions. Compiler knows where to locate this file. Not a C++
statement.
● Namespace is a new concept introduced in the ANSI (American
National Standard Institute ) C++ committee. It provides the scope to
the variables and hold them globally.
● std is the namespace where ANSI standard class libraries are defined.
● Keywords: the words which are reserved in C++. e.g - iostream, std,
using, include, cout, int, return …
Output Operator

● cout is the output operator. Which has the following syntax:

cout<< any expression;

● << is the insertion operator, which feeds input to cout.


● endl is operator to end a line. That sends the cursor to next line in the
output.
Introuduction
● Every program contains the function main() , which contains the executable
part of the program.
● The main block is enclosed within curly {} braces.
● It has a return type, by default the return type is int.
● return 0, return 0 on successful execution of the code.
● Every statement is succeeded by a semicolon (;) which marks the end of the
statement.
Comments:
Comments:
TOKENS: The smallest individual unit in a
program is called token.
● Keywords
● Identifiers
● Constants
● Strings
● Operators
Keywords:
● Reserved identifiers that can not be used as variable name of user defined
function names.
Identifiers:
Identifiers are the name of the user defined variable or the functions. They should
follow the following rules:
Data Types:
Data Storage:

Each data is storage in the form of bits (0/1) in computer. Suppose we can represent
a particular data type in 8 number of bits:

Then the largest number it can store is 11111111 (28 -1 ) and smallest number is
00000000 (equals to 0).

If the data type is signed (+ or -), then one bit is reserved for sign and range of
data is (-28-1 to 28-1 -1).
Bit/Byte
1 Bit = 0/1

1 Byte = 8 bits

2 Byte = 16 bits

4 Byte = 32 bits
Data Types and their sizes
Integer Type:
Syntax: int varibale_name

Size - 4 Bytes (Range : -231 to 231-1).

Variable Declaration:

int x;

x is declared as integer type variable.

Variable Initialization:

x = 5;

x is assigned the value 5. (= is the assignment operator)


Example:

We can also define as: Or can also declare and initialize together:
int x = 3;
int x, y; int y = 4;
Integer Overflow:
bool type:
● bool type data is used to represent logical TRUE (1) or FALSE (0)
● They are integer type of data.
char type:
● Individual characters can be stored in char type variables.
ASCII Value
Printing ASCII values:
String :
Textual data can be handled by arrays of characters or by the data type string.
Floating point numbers:

● The maximum number of significant digits, i.e., the number of decimal place, in float values is 6 or 7, while in
double type is 15. The maximum number of significant digits is called precision .
● Why not use float or double type always in place of int type?
Arithmetic Operators
If a operator is applied on same type of data, then
the output is also of same type.
● If we want to have output of x/y = 16/5 equals to 3.2, then we have to
convert, at least one of x or y to float or double. Which can be done as

float(x) or float(y) , this process is called type casting.

Type casting does not change the type of variable.


Numbers without decimal are considered as integers and with decimals as
float/double.
Mod operator:
Operator Precedence:
Mixed Expressions:
Arithmetic operator with assignment operator:

x++ or ++x is same as x = x+1 or x +=1

x-- or --x is same as x = x -1 or x - =1


Preincrement and postincrement operator
y = x++ , means assign x to y and increase x by 1.

y = ++x, means increase x by 1 and assign it to y.

y = x-- or y = --x, behaves similarly.


Library required:

#include<cmath>
Comparison/Relational and Logical Operators:
Complex Numbers:
Library: #include<complex>

A complex number is of type a+ib, where a and b can be both long or double.

E.g.

complex<double> z1; (z1 is a complex numbers with both real and imaginary parts
as double).

complex<long> z2 (z2 is a complex numbers with both real and imaginary parts as
long).
typedef
In C++, typedef ( type definition ) is a keyword allows us to create an alias for existing data types. Once, we have created a
type definition, then we can declare a variable using the alias we created.

typedef existing_type new_name;

E.g.

typedef int my_type;

my_type var1, var2, var3;


Creating header file: (save as complexx.h)
/**
* @file complexx.h
* @brief A header file that adds convenient extensions for working
* with complex numbers.
*/
#ifndef COMPLEXX_H
#define COMPLEXX_H
#include <complex>
using namespace std;
/**
* We define C to be an abbreviation for complex<double>.
*/
typedef complex< double> C;
/**
* We define i to be a constant equal to sqrt(-1), i.e., C(0.,1.).
*/
const C i = C( 0., 1.);
#endif
#include <iostream>
#include <complex>
#include "complexx.h"

using namespace std;


int main(){
C z1;
C z2;
C z3;
z1 = complex< long>(4, 5);
z2 = complex< double>(6.0, 9.0);
z3 = complex< long>(5, 6);
cout<< "z1 = " <<z1<<endl;
cout<< "z2 = "<<z2<<endl;
cout<< "z1+z3 = " <<z1+z3<<endl;
return 0;
}
Variable and their names:
Read from book.

Give meaningful Names.

Don’t used keywords.

Can contain lowercase, uppercase, number and underscore in variable name.

Variable name cannot start with a digit.

You might also like