Chapter 3

You are on page 1of 26

Classes and Objects

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 member variable of structure can not


The member variable of class can be be initialized directly.
initialized directly.
Structure object can be created without
class object can not be created without using using the new keyword.(optional)
the new keyword, it means we have to use it.
Demo obj;
Demo obj=new Demo();
Class
Access specifier
• Access specifiers in C++ class defines the access control rules. C++ has 3 new
keywords introduced, namely,
1. public
2. private
3. protected
• These access specifiers are used to set boundaries for availability of members of
class be it data members or member functions
• Access specifiers in the program, are followed by a colon. You can use either one,
two or all 3 specifiers in the same class to set different boundaries for different class
members. They change the boundary for all the declarations that follow them.
• Public
• Public, means all the class members declared under public will be available to
everyone. The data members and member functions declared public can be
accessed by other classes too. Hence there are chances that they might change them.
So the key members must not be declared public.
• class PublicAccess
• { public: // public access specifier
• int x; // Data Member Declaration
• void display(); // Member Function decaration
• }
• Private
• Private keyword, means that no one can access the class members declared private
outside that class. If someone tries to access the private member, they will get a
compile time error. By default class variables and member functions are private.
• class PrivateAccess {
• private: // private access specifier
• int x; // Data Member Declaration
• void display(); // Member Function decaration
• }
• Protected
• Protected, is the last access specifier, and it is similar to private, it makes class
member inaccessible outside the class. But they can be accessed by any subclass of
that class. (If class A is inherited by class B, then class B is subclass of class A. We
will learn this later.)
• class ProtectedAccess
• { protected: // protected access specifier
• int x; // Data Member Declaration
• void display(); // Member Function decaration
• }
Creating objects

• 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).

• The benefits of inline functions are as follows :


– 1. Better than a macro.
– 2. Function call overheads are eliminated.
– 3. Program becomes more readable.
– 4. Program executes more efficiently.
Static data member
• It is generally used to store value common to the
whole class. The static data member differs from an
ordinary data member in the following ways :
• (i) Only a single copy of the static data member is used
by all the objects.
• (ii) It can be used within the class but its lifetime is the
whole program.

• For making a data member static, we require :


• (a) Declare it within the class.
• (b) Define it outside the class.
• For example
• Class student
• {
• Static int count; //declaration within class
• -----------------
• -----------------
• -----------------
• };
• The static data member is defined outside the class as :
• int student :: count; //definition outside class
• The definition outside the class is a must.
• We can also initialize the static data member at the time of its definition as:
• int student :: count = 0;
• If we define three objects as : sudent obj1, obj2, obj3;
static member function
• A static member function can access only the static members of a class. We can do
so by putting the keyword static before the name of the function while declaring it
for example,
• Class student
• {
• Static int count;
• -----------------
• public :
• -----------------
• -----------------
• static void showcount (void) //static member function
• {
• Cout<<”count=”<<count<<”\n”;
• }
• };
• int student ::count=0;
• Here we have put the keyword static before the name of
the function shwocount ().
• In C++, a static member function fifers from the other
member functions in the following ways:
• (i) Only static members (functions or variables) of the same
class can be accessed by a static member function.
• (ii) It is called by using the name of the class rather than an
object as given below:

• Name_of_the_class :: function_name
• For example,
• student::showcount();

You might also like