unit-1-new-cpp
unit-1-new-cpp
unit-1-new-cpp
Evolution of C++
C++ is an object oriented language
It is an extension of C language
C++ was developed by Bjarne Stroustrup at AT&T Bell
laboratories in Murray Hill, New Jersey, USA during 1980's.
Stroustrup combined the features of Simula67 and C into a more
powerful language that could support object-oriented
programming with features of C.
C++ is an extension of C with a major addition of the class
construct features of Simula67
Stroustrup initially called the new language 'C with classes’. In
1983 the name was changed to C++. The idea of C++ comes from
the C increment operator. Mascitti coined the term C++ in 1983.
All the concepts of C are applicable to C++. During 1990's the
language underwent a number of improvements and changes.
The most important facilities that C++ adds on to C are classes,
inheritance, function overloading and operator overloading.
These features enable creating of abstract data types, inherit
properties from existing data types and support polymorphism,
thereby making C++ a truly object oriented Language.
The object Oriented features in C++ allow programmers to build
large programs with clarity, extensibility and ease of maintenance.
ANSI Standard
ANSI stands for American National Standards Institute.
The Institute ANSI was founded in 1918.
The main goal for establishing this Institute was to suggest,
reform, recommend and publish standards for data processing
in the USA. This committee sets up standards for the computer
industry.
The ANSI / ISO council has made an international standard for
C++ (ISO - International Standards Organization). The first draft
of the planned ANSI standard was made on 25 January 1994.
In November 1997, the language underwent a number of
improvements and changes, and ANSI/ISO committee
standardised and added several new features.
The ANSI standard is an attempt to ensure that C++ is portable.
1. Class
2. Object
3. Abstraction
4. Encapsulation
5. Data Hiding
6. Inheritance
7. Reusability
8. Polymorphism
9. Virtual Functions
10.Message passing
Class: Class is an abstract data type (user defined data type) that
contains member variables and member functions that operate on
data. It starts with the keyword class. A class denotes a group of
similar objects.
i) Procedural Abstraction
ii) Data Abstraction
Data Hiding: All the data in a class can be restricted from using it
by giving some access levels (visibility modes). The three access
levels are private, public, protected.
Private data and functions are available to the public
functions only. They cannot be accessed by the other part
of the program. This process of hiding private data and
functions from the other part of the program is called as
data hiding.
Inheritance: Inheritance is the process of acquiring (getting) the
properties of some other class. The class whose properties are
being inherited is called as base class and the class which is
getting the properties is called as derived class.
poly many
morphism forms
C++
Smalltalk
Charm ++
Java
SMALLTALK
Smalltalk is a pure object oriented language. C++ makes few compromises to ensure
quick performance and small code size. Smalltalk uses run-time binding. Smalltalk
programs are considered to be faster than the C++. Smalltalk needs longer time to learn
than C++. Smalltalk programs are written using Smalltalk browser. Smalltalk uses
dynamic objects and memory is allocated from free store. It also provides automatic
garbage collection and memory is released when object is no longer in use.
CHARM++
Charm ++ is also an object oriented programming language. It is a portable. The
language provides features such as inheritance, strict type checking, overloading, and
reusability. It is designed in order to work efficiently with different parallel systems
together with shared memory systems, and networking.
Programming paradigms
Monolithic programming paradigm
Structured-oriented programming paradigm
Procedure-oriented programming paradigm
Object-oriented programming paradigm
Include files : It controls the header files for function declaration. Each header
file has an extension .h
e.g # include “ iostream.h” (or)
# include <iostream.h>
iostream.h file defines all the definitions and prototypes of functions. The header
file also gets compiled with the original program.
Class declaration or definition: A class is defined in this section. It contains
data variable, data member, function prototypes or function definitions. The
class definition is always terminated by a semi-colon.
Class function definition :This part contains definition of functions. The
function definition can be done outside or inside the class. the functions defined
inside the class are implemented as inline functions. when a function is large, the
function prototype is declared Inside the class and defined outside the class.
main() function: C++ program always start execution from main().
Structure Of A Program :
#include <iostream.h>
int main ()
{
cout << "Hello World!"; return 0;
}
Output:-Hello World!
We are going to look line by line at the code we have just written:
// my first program in C++
This is a comment line. All lines beginning with two slash signs (//) are considered comments..
The programmer can use them to include short explanations or observations within the source
code itself.
#include <iostream.h>
Lines beginning with a hash sign (#) are directives for the preprocessor. In this case the directive
#include<iostream.h> tells the preprocessor to include the iostream standard file. This specific
file (iostream) includes the declarations of the basic standard input-output library in C++.
int main ()
This line corresponds to the beginning of the definition of the main function. The main
function is the point by where all C++ programs start their execution. It is essential that all C++
programs have a main function.
The word main is followed in the code by a pair of parentheses (()). That is because it is a
function declaration. Right after these parentheses we can find the body of the main function
enclosed in braces ({}). What is contained within these braces is what the function does when it
is executed.
cout << "Hello World!";
cout represents the standard output stream in C++, and the meaning of the entire statement
is to insert a sequence of characters (in this case the Hello World sequence of characters) into the
standard output stream (which usually is the screen).
The statement ends with a semicolon character (;).
return 0;
The return statement causes the main function to finish. A return code of 0 for the main function
is generally interpreted as the program worked as expected without any errors during its
execution. This is the most usual way to end a C++ console program.
· Example :-
# include<iostream.h>
class person
{
char name[30];
int age;
public:
void getdata(void);
void display(void);
};
(ii)IDENTIFIERS:
Identifiers refers to the name of variable , functions, arrays, classes,
objects etc. created by programmer.. The following rules are used for
naming the identifiers,.
1. Only alphabetic chars, digits and under score are
permitted.
2. The name can’t start with a digit.
3. Upper case and lower case letters are distinct.
4. A declared keyword can’t be used as a variable name.
In ANSI C the maximum length of a variable is 32 chars but in C++
there is no bar.
(iii) Constants
Constants are values to the variable, which do not change during
the execution of a program.
Constants are also like normal variables. only difference is, their
values can not be modified by the program once they are
defined. Constants refer to fixed values. They are also called as
literals.
Constants may belong to any of the data type.
Two types of constants, Literal constant and symbolic constants.
Literal constant contain 3 categories. They are string constant,
numeric constant and charcter constant.
Syntax:
const data_type variable_name; (or)
const data_type *variable_name;
EXAMPLES:
Integer constants – Example: 0, 1, 1218, 12482
Real or Floating point constants – Example: 0.0, 1203.03, 30486.184
Octal & Hexadecimal constants – Example: octal: 013
Hexadecimal: 0X13
Character constants -Example: ‘a’, ‘A’, ‘z’
String constants -Example: “nicas”
(iv) operators
The process of initializing a variable at the moment it is declared at runtime is called dynamic
initialization of the variable. Thus, during the dynamic initialization of a variable, a value is
assigned to execution when it is declared.
Example program :
# include<iostream.h>
void main()
{
int a;
cout<<“Enter Value of a”;
cin>>a;
int cube = a * a * a;
}
In the above example, the variable cube is initialized at run time using expression a * a * an at the
time of its declaration.
________________________________________________
BASIC DATA TYPES IN C++
Both C and C++ compilers support all the built in types. With the exception of void the basic
datatypes may have several modifiers preceding them to serve the needs of various situations.
The modifiers signed, unsigned, long and short may applied to character and integer basic data
types. However the modifier long may also be applied to double.
3 basic types: integer, float and void
(i) Integer
Integers are whole numbers
Integers Occupy 1 word of storage( 1 word= 2 bytes /16 bits)
C++ defines this type as consisting of the values ranging from -32768 to 32767. (-215
to 215)
3 classes of int type: short int, int, long int
Long and unsigned int are used to increase the range of values.
Signed is default
If long integer is needed the type long or long int can be used. The range of long int
is too big that is from -2147483648 to 2147483647, which occupies 4 bytes in
memory.
(ii) float
C++ defines the data type float as representing numbers that have fractional part.
Floating point variables can either be small or large.
A variable with type float occupies 4 bytes in size with 6 digits of precision
Floating type number are Known as single precision numbers
Example: float f;
(iii)double
When the accuracy provided by float number is not sufficient, the type double
can be used.
A double data type uses 64 bits giving a precision of 14 digits.
14 digits precision
Long double takes 80 bits (10 bytes)
Known as double precision numbers
Example: double d;
(iv)Char
Ch = ‘a’;
Void type
void function(void);
~~~~~~~~~~~~~~~~~~~~~~~~~~
FunctionType FunctionName(parameters)
{
}
Example program:
#include <iostream.h>
Output:
m is 20
2. Array
An array is a set of elements that are kept in memory in a continuous
manner and also have the same type of data present within the array.
The purpose of an array is to store a lot of data in a single variable
name and sequential order.
Syntax:
DataType ArrayName[size_of_array];
Example:
int arr[4]={0,1,2,3};
#include <iostream.h>
int main()
{
int arr[5]={10, 30, 20, 40, 30}; //creating and initializing
array
//traversing array
for (int i=0; i<5: i++)
{
cout<<arr[i]<<"\n";
}
}
Output:
10
30
20
40
30
3. Pointer
Syntax:
data_type *variable_name;
Example:
int *ptr;
Example program:
#include <iostream.h>
#include<conio.h>
void main()
{
int var = 20;
Output:
Value at ptr = 0x7ffc10d7fd5c
Value at var = 20
Value at *ptr = 20
4. Reference Variables
Class
Structure
Union
Enumeration
Typedef defined DataType
1. Class: The building block of C++ that leads to Object-Oriented programming is
a Class. It is a user-defined data type, which holds its own data members and
member functions, which can be accessed and used by creating an instance of that
class. A class is like a blueprint for an object.
Syntax:
Example:
#include <iostream.h>
#include<conio.h>
class scaler
{
public:
string student_name;
void print_name()
{
cout << "Student name is: " << student_name << endl;
}
};
int main()
{
scaler student1, student2;
clrscr();
student1.student_name = "Shivam Singla";
student1.print_name();
student2.student_name = "Sachin Singla";
student2.print_name();
return 0;
}
2. STRUCTURES
struct Person
{
char name[30];
int citizenship;
int age;
}
Example 1:
#include <iostream.h>
struct Person
{
int citizenship;
int age;
};
int main(void)
{
struct Person p;
p.citizenship = 1;
p.age = 27;
cout << "Person citizenship: " << p.citizenship << endl;
cout << "Person age: " << p.age << endl;
return 0;
}
Output:
3. Union
An enumerated data type is another user defined type which provides a way for
attaching names to number, these by increasing comprehensibility of the code. The enum
keyword automatically enumerates a list of words by assigning them values 0,1,2 and soon.
This facility provides an alternative means for creating symbolic.
We can modify some of the fundamental data types using modifiers with them. C++ offers 4 modifiers:
1. signed
2. unsigned
3. short
4. long
1. signed Modifier
Example:
Here,
Example:
C++
#include <iostream>
// Driver Code
int main()
return 0;
Output
Note: The int datatype is signed by default. So, int can be directly be used instead of signed int.
2. unsigned Modifier
Unsigned variables can store only non-negative integer values.
Example:
unsigned int a = 9;
unsigned int b = 0;
Here,
Example:
C++
#include <iostream>
// Driver Code
int main()
return 0;
Output
The signed value of the signed variable is stored in the allocated memory. However, the
signed value is not stored by the unsigned variables.
The sign takes 1 bit extra. So, if the unsigned value is being used then one-bit extra space is
used to save the value of a variable.
The range of values for unsigned types starts from 0. For example, for unsigned int, the value
range is from 0 to 4,294,967,295. However, for signed int, the value range is from -
2,147,483,648 to 2,147,483,647.
Note: signed and unsigned modifiers can only be used with int and char datatypes.
3. short Modifier
The short keyword modifies the minimum values that a data type can hold. It is used for small
integers that lie in the range of −32,767 to +32,767.
Example:
Example:
C++
#include <iostream>
// Driver Code
int main()
Output
Note: The short int can be written as short also. They are equivalent.
4. long Modifier
The long keyword modifies the maximum values that a data type can hold. It is used for large integers
that are in the range of -2147483647 to 2147483647.
Example:
Example:
C++
#include <iostream>
// Driver Code
int main()
return 0;
Output
. Literals (Constants)
Literals are data items whose values do not change during the execution of a program.
Therefore Literals are called as Constants. C++ has several kinds of literals:
Numeric Constants:
As the name indicates, the numeric constants are numeric values, which are used as
constants. Numeric constants are further classified as:
• Integer Constants (or) Fixed point constants.
Integers are whole numbers without any fractions. An integer constant must have at
least one digit without a decimal point. It may be signed or unsigned. Signed integers
are considered as negative, commas and blank spaces are not allowed as part of it. In C+
+, there are three types of integer constants: (i) Decimal (ii) Octal (iii) Hexadecimal
(i) Decimal
If you assign 4.56 as an integer decimal constant, the compiler will accept only the
integer portion of 4.56 ie. 4. It will simply ignore .56.
If a Decimal constant declared with fractions, then the compiler will take only the
integer part of the value and it will ignore its fractional part. This is called as “Implicit
Conversion”. It will be discussed later.
(ii) Octal
Any sequence of one or more octal values (0 …. 7) that begins with 0 is considered as
an Octal constant.
When you use a fractional number that begins with 0, C++ has consider the number as
an integer not an Octal.
(iii) Hexadecimal
The suffix L or l and U or u added with any constant forces that to be represented as a
long or unsigned constant respectively.
Example:
Boolean Literals
Boolean literals are used to represent one of the Boolean values(True or false).
Internally true has value 1 and false has value 0.
Character constant
A character constant is any valid single character enclosed within single quotes. A
character constant in C++ must contain one character and must be enclosed within a
single quote.
The value of a single character constant has an equivalent ASCII value. For example,
the value of ‘A’ is 65.
ASCII (American Standard Code for Information Interchange) was first developed and
published in 1963 by the X3 committee, a part of the American Standards Association
(ASA).
String Literals
Sequence of characters enclosed within double quotes are called as String literals. By
default, string literals are automatically added with a special character ‘\0’ (Null) at the
end. Therefore, the string “welcome” will actually be represented as “welcome\0” in
memory and the size of this string is not 7 but 8 characters i.e., inclusive of the last
character \0.