Upload
jatin-batra
View
63
Download
0
Embed Size (px)
Citation preview
45 Days C++ Programming Training
In Ambala
www.batracomputercentre.com
Email id: [email protected] Ph. No.: 9729666670, 4000670
Introduction To C++• C++ is a middle-level programming language
developed by Bjarne Stroustrup starting in 1979 at Bell Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
History of C++• C++ Development started in 1979.During the creation of Ph.D.
thesis, Bjarne Stroustrup worked with language called Simula.• Bjarne Stroustrup identified that this OOP features can be
included in the software development• C++ is also called as C with classes• Stroustrup states that the purpose of C++ is to make writing good
programs easier and more pleasant for the individual programmer.
• After that Bjarne Stroustrup started working on the C language and added more extra OOP features to the classic C.He added features in such a fashion that the basic flavor of C remains unaffected.
• C++ programming language is extension to C Language. In C we have already used increment operator (++). Therefore we called C++ as “Incremented C” means Extension to C.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Characteristics Of C++• C++ Provides huge Function Library that’s why its
popularity is increasing day by day and more programmers are inclining towards C++ due to its multiple features –
• C++ is an Object Oriented Programming Language (OOPL).• C++ have huge Function Library• C++ is highly Flexible language with Versatility.• C++ can be used for developing System Software viz.,
operating systems, compilers, editors and data bases.• C++ is suitable for Development of Reusable Software. ,
thus reduces cost of software development.• C++ is a Machine Independent Language.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Features Of C++1. Objects2. Classes3. Abstraction4. Encapsulation5. Inheritance6. Overloading7. Exception Handling
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Objects • Objects are the basic unit of OOP. They are
instances of class, which have data members and uses various member functions to perform tasks.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Classes• Class can also be defined as user defined
data type but it also contains functions in it. So, class is basically a blueprint for object. It declare & defines what data variables the object will have and what operations can be performed on the class's object.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Abstraction• Abstraction refers to showing only the
essential features of the application and hiding the details. In C++, classes provide methods to the outside world to access & use the data variables, but the variables are hidden from direct access. This can be done access specifiers.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Encapsulation• It can also be said data binding.
Encapsulation is all about binding the data variables and functions together in class.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Inheritance• Inheritance is a way to reuse once written
code again and again. The class which is inherited is called base calls & the class which inherits is called derived class. So when, a derived class inherits a base class, the derived class can use all the functions which are defined in base class, hence making code reusable.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Overloading• It is a feature, which lets us create functions
with same name but different arguments, which will perform differently. That is function with same name, functioning in different way. Or, it also allows us to redefine a function to provide its new definition. You will learn how to do this in details soon in coming lessons.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Variables In C++• A variable provides us with named storage that our
programs can manipulate. • Each variable in C++ has a specific type, which
determines the size and layout of the variable's memory.• the range of values that can be stored within that
memory;• the set of operations that can be applied to the variable.• The name of a variable can be composed of letters,
digits, and the underscore character. • It must begin with either a letter or an underscore.• Upper and lowercase letters are distinct because C++ is
case-sensitive.Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Constants In C++• Constants refer to fixed values that the
program may not alter and they are called literals.
• Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
1) User Defined Data Types
• These are the type, that user creates as a class. In C++ these are classes where as in C it was implemented by structures.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
a) Structures• Structure is the collection of variables of different
types under a single name for better visualization of problem and can hold data of one or more types.
• The struct keyword defines a structure type followed by an identifier(name of the structure). Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example:
struct person { char name[50]; int age; float salary; };
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
b) Unions• Unions allow one portion of memory
to be accessed as different data types. Its declaration and use is similar to the one of structures, but its functionality is totally different. For Example:
union type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names;
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
c) EnumerationEnumerated types are types that are defined with a set of custom identifiers, known as enumerators, as possible values. Objects of these enumerated types can take any of these enumerators as value.
Their syntax is:
This creates the type type_name, which can take any of value1, value2, value3, ... as value. Objects (variables) of this type can directly be instantiated as object_names.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
d)Class• C++ offers a new user-defined data type
known as class, which forms the basis of object-oriented programming. A class acts as a template which defines the data and functions that are included in an object of a class. Classes are declared using the keyword class. Once a class has been declared, its object can be easily created.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
2) Built In Data Type• The basic (fundamental) data types provided by
C++ are integral, floating point and void data type. Among these data types, the integral and floating-point data types can be preceded by several type modifiers.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
a)Int• Numbers without the fractional part
represent integer data. In C++, the int data type is used to store integers such as 4, 42, 5233, -32, -745. Thus, it cannot store numbers such as 4.28, -62.533.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
b)Char• Characters refer to the alphabet, numbers
and other characters (such as {, @, #, etc.) defined in the ASCII character set. In C++, the char data type is also treated as an integer data type as the characters are internally stored as integers that range in value from -128 to 127
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
c) Float• A floating-point data type is used to store
real numbers such as 3 .28, 64. 755765, 8.01, -24.53. This data type includes float and double' data types.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
3) Derived Data Types• Data types that are derived from the built-in
data types are known as derived data types. The various derived data types provided by C++ are arrays, functions, pointers and references.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
a)Array• An array is a set of elements of the same data
type that are referred to by the same name. All the elements in an array are stored at contiguous memory locations and each element is accessed by a unique index or subscript value. The subscript value indicates the position of an element in an array.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
b)Functions• A function is a self-contained program
segment that carries out a specific well-defined task. In C++, every program contains one or more functions which can be invoked from other parts of a program, if required.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
c)Pointers• A pointer is a variable that can store the
memory address of another variable. Pointers allow to use the memory dynamically. That is, with the help of pointers, memory can be allocated or de-allocated to the variables at run-time, thus, making a program more efficient.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
d)References• A reference is an alternative name for a
variable. That is, a reference is an alias for a variable in a program. A variable and its reference can be used interchangeably in a program as both refer to the same memory location
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Loops in C++1. Loop control statements change execution
from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Loop Type Description
For Loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
While LoopRepeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
Do while loopLike a while statement, except that it tests the condition at the end of the loop body
Nested Loops You can use one or more loop inside any another while, for or do..while loop.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Decision Making Statements
• Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Statements Description
If Statement An if statement consists of a boolean expression followed by one or more statements.
If… Else Statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false.
Switch StatementsA switch statement allows a variable to be tested for equality against a list of values.
Nested if statements You can use one if or else if statement inside another if or else if statement(s).
Nested Switch Statements
You can use one switch statement inside another switch statement(s).
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Basic Input/output• The C++ standard libraries provide an extensive set of
input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming.
• C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc, this is called output operation.
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Output Stream The predefined object cout is an instance of ostream class. The cout object is said to be "connected to" the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following example. cout << "Value of str is : " << str << endl;
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Input Stream The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following example. cin >> name;
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
C++ Programming Language
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.comwww.batracomputercentre.com
C++ Programming Language
• Introduction• Classes, Objects• Inheritance• Polymorphism• Overloading• Encapsulation• Constructor &
Destructor• Function
• Pointers Structure
• Arrays & Strings• Structures, Union
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com
SCO-15 Dayal Bagh, Near Panchmukhi Hanuman Mandir,Ambala Cantt- 133001HaryanaPh. NO. : 4000670, 97296666770
www.batracomputercentre.com
Email id: [email protected] Ph. No.: 9729666670, 4000670
Email id: [email protected] Ph. No.: 9729666670, 4000670
www.batracomputercentre.com