Chapter 3
Chapter 3
Chapter 3
Introduction to C++
• C++ is an object-oriented programming language. It was developed by Bjarne
Stroustrup at AT&T Bell Laboratories in Murray Hill, New Jersey, USA, in the early
1980’s.
• Stroustrup, an admirer of Simula67 and a strong supporter of C, wanted to
combine the best of both the languages and create a more powerful language that
could support object-oriented programming features and still retain the power and
elegance of C.
• The result was C++. Therefore, C++ is an extension of C with a major addition of
the class construct feature of Simula67.
• Since the class was a major addition to the original C language, Stroustrup initially
called the new language ‘C with classes’.
• However, later in 1983, the name was changed to C++. The idea of C++ comes
from the C increment operator ++, thereby suggesting that C++ is an augmented
version of C.
Simple C++ Program
• Let us begin with a simple example of a C++
program that prints a string on the screen.
Printing A String
• #include<iostream>
• Using namespace std;
• int main()
• {
• cout<<” c++ is better than c \n”;
• return 0;
• }
Input/output operator of c++
• 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
•
Input/output operator of c++
• 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. Thecin 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.
•
Difference between structure and class
Class Structure
Class is a reference type and its object is Structure is a value type that is why its
created on the heap memory. object is created on the stack memory.
Class can inherit the another class. Structure does not support the inheritance.
Class can have the all types of constructor Structure can only have the parameterized
and destructor. constructor. it means a structure can not
have the non-parameterized constructor,
default constructor and destructor also.
• The objects of a class are declared after the class definition. One must remember
that a class definition does not define any objects of its type, but it defines the
properties of a class. For utilizing the defined class, we need variables of the class
type. For example,
• Largest ob1,ob2; //object declaration
• will create two objects ob1 and ob2 of largest class type. As mentioned earlier, in
C++ the variables of a class are known as objects. These are declared like a
simple variable i.e., like fundamental data types.
• In C++, all the member functions of a class are created and stored when the class is
defined and this memory space can be accessed by all the objects related to that
class.
• Memory space is allocated separately to each object for their data members.
Member variables store different values for different objects of a class.
ACCESSING MEMBERS FROM
OBJECT(S)
• After defining a class and creating a class variable i.e., object we can access the data members
and member functions of the class. Because the data members and member functions are parts
of the class, we must access these using the variables we created. For functions are parts of
the class, we must access these using the variable we created. For Example,
• Class student
• {
• private:
• char reg_no[10];
• ` char name[30];
• int age;
• char address[25];
• public :
• void init_data()
• {
• - - - - - //body of function
• -----
• }
• void display_data()
• }
• };
• student ob; //class variable (object) created
• -----
• -----
• Ob.init_data(); //Access the member function
• ob.display_data(); //Access the member function
• Here, the data members can be accessed in the member
functions as these have private scope, and the member
functions can be accessed outside the class i.e., before or
after the main() f
Defining a member function outside
class
Defining a member function inside
class
Inline function
• These are the functions designed to speed up program
execution. An inline function is expanded (i.e. the function
code is replaced when a call to the inline function is made) in
the line where it is invoked.
• In case of normal functions, the compiler have to jump to
another location for the execution of the function and then the
control is returned back to the instruction immediately after the
function call statement.
• So execution time taken is more in case of normal functions.
There is a memory penalty in the case of an inline function.
The system of inline function is as follows :
inline function_header
{
body of the function
}
For example,
//function definition min()
inline void min (int x, int y)
cout<< (x < Y? x : y);
}
Void main()
{
int num1, num2;
cout<<”\Enter the two intergers\n”;
cin>>num1>>num2;
min (num1,num2; //function code inserted here
------------------
------------------
}
• An inline function definition must be defined before being invoked as
shown in the above example. Here min ( ) being inline will not be called
during execution, but its code would be inserted into main ( ) as shown
and then it would be compiled.
• If the size of the inline function is large then heavy memory pentaly makes
it not so useful and in that case normal function use is more useful.
• The inlining does not work for the following situations :
• 1. For functions returning values and having a loop or a switch or a goto
statement.
• 2. For functions that do not return value and having a return statement.
• 3. For functions having static variable(s).
• 4. If the inline functions are recursive (i.e. a function defined in terms of
itself).
• Name_of_the_class :: function_name
• For example,
• student::showcount();