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

Module2 1

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module2 1

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

Amity School of Engineering & Technology

Object Oriented Programming


using
Amity School of Engineering & Technology

2
Amity School of Engineering & Technology

Classes & Objects


Contents Amity School of Engineering & Technology

 Object & classes, attributes, methods


 C++ class declaration
 Local Class and Global Class
 State identity and behaviour of an object
 Local Object and Global Object
 Scope Resolution Operator
 Friend Function
 Inline Functions
 Constructors and Destructors
 Types of Constructors
 Static Class Data
 Array of Objects
 Member functions and Objects
Amity School of Engineering & Technology

• Learn about classes Objectives


• Learn about private, protected, and public members of a class
• Explore how classes are implemented
• Examine constructors and destructors
• Learn about information hiding
• Explore how information hiding is implemented in C++
• Learn about the static members of a class
Object & classes Amity School of Engineering & Technology

 Class:
 A class is a definition of objects of the same kind.
 A class is a blueprint, template, or prototype that defines and describes the static
attributes and dynamic behaviors common to all objects of the same kind.
 Object:
 It is an instantiation of a class.
 Object is an instance of a class.
 All the members of the class can be accessed through object.
 Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.
 Object is an entity that has state and behavior.
 Here, state means data and behavior means functionality.
Amity School of Engineering & Technology

Define a Class Type


Header class Rectangle
class class_name {
{ private:
permission_label:
int width;
member;
Body permission_label:
int length;
member; public:
... void set(int w, int l);
}; int area();
};
7
Amity School of Engineering & Technology

Classes & Objects


Objects: Instance of a class
class Rectangle
{
Rectangle r1;
private: Rectangle r2;
int width; Rectangle r3;
int length;

……
public:
void set(int w, int l);
int area(); r1.width=12;
}; r1.length=12;

8
Object & classes Amity School of Engineering & Technology

Creating a Class Box Creating Objects of class Box

class Box Box Box1; // Declare Box1 of type Box


{ Box Box2; // Declare Box2 of type Box
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
C++ Class declaration Amity School of Engineering & Technology

class Test
{
private: This class has:
int data1;
float data2;
Two data members:
public:
data1
void function1() data2
{ Two member functions:
data1 = 2; function1()
}
float function2()
function2()
{
data2 = 3.5;
return data2;
}
};
Amity School of Engineering & Technology

Access Specifiers/ Modifiers in C++


• Access modifiers are used to implement an important feature of
Object-Oriented Programming known as Data Hiding.
• There are 3 types of access modifiers available in C++:
• Public
• Private
• Protected
• by default the access modifier for the members will be Private.
Amity School of Engineering & Technology

Public:
• 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.
Amity School of Engineering & Technology

#include<iostream> // main function


using namespace std; int main()
class Circle {
{ Circle obj;
public: obj.radius = 5.5;
double radius; cout << "Radius is: " << obj.radius
double compute_area() << "\n";
{ cout << "Area is: " <<
obj.compute_area();
return 3.14*radius*radius;
return 0;
}
}
};
Amity School of Engineering & Technology

Private:
• The class members declared as private can be accessed
only by the functions inside the class. They are not
allowed to be accessed directly by any object or function
outside the class. Only the member functions or
the friend functions are allowed to access the private
data members of a class.
Amity School of Engineering & Technology

#include<iostream> // main function


using namespace std; int main()
class Circle {
{
Circle obj;
private:
double radius;
public:
// trying to access private data member
double compute_area() // directly outside the class not allowed
{ obj.radius = 1.5;
return 3.14*radius*radius; cout << "Area is:" << obj.compute_area();
} return 0;
}
};
Amity School of Engineering & Technology

Protected:
• Protected access modifier is similar to that of private
access modifiers, the difference is that the class member
declared as Protected are in accessible outside the class
but they can be accessed by any subclass(derived class)
of that class.
Amity School of Engineering & Technology

C++ Access Specifiers


Specifiers Within Same In Derived Outside the
Class Class class

Private Yes No No

Protected Yes Yes No

Public Yes Yes Yes


Amity School of Engineering & Technology

C++ Access Specifiers


• By default, all members of a class are private.
• If a member of a class is private, you cannot access it outside of
the class.
• A public member is accessible outside of the class.
• To make a member of a class public, you use the member
access
• specifier public with a colon, :
Amity School of Engineering & Technology

C++ Access Specifiers


class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Methods in C++ Amity School of Engineering & Technology

• Methods are functions that belongs to the class.

• There are two ways to define functions that belongs to a class:


o Inside class definition

o Outside class definition


Methods in C++ Amity School of Engineering & Technology

Inside Class Definition Outside Class Definition


class MyClass { // The class class MyClass { // The class
public: // Access specifier public: // Access specifier
void myMethod() { void myMethod(); // Method/function declaration
// Method/function defined inside the class };
cout << "Hello World!"; // Method/function definition outside the class
} void MyClass::myMethod() {
}; cout << "Hello World!";
int main() { }
int main() {
// Create an object of MyClass MyClass myObj; // Create an object of MyClass
MyClass myObj; myObj.myMethod(); // Call the method
myObj.myMethod(); // Call the method return 0;
return 0; }
}
Amity School of Engineering & Technology
Define a Member Function
class Rectangle
{
private:
int width, length;
class name
public:
void set (int w, int l);
int area() {return width*length; } member function name

};

void Rectangle :: set (int w, int l)


inline {
r1.set(5,8);
width = w;
rp->set(8,10); length = l;
scope operator
}
22
Scope Resolution Operator
Amity School of Engineering & Technology

 Scope resolution operator (::) in C++ is used:


 To define a function outside a class
 When we want to use a global variable but also has a local variable with the same name.
#include <iostream>
using namespace std;
char c = 'a'; Output
// global variable (accessible to all functions) Local Variable: b
int main() { Global Variable: a
char c = 'b';
// local variable (accessible only in main function)
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n";
// Using scope resolution operator
return 0; }
Amity School of Engineering & Technology

Constructors
• A constructor is a Special member function which is used to initialize
data members
• It must be a public member function
• It must be named the same as the class
• It must have no return type
• automatically called when an object of the class is created
Amity School of Engineering & Technology

Examples

Inline: Declaration outside the class:


class Square Square( int ); //prototype
{ // in class
...
public: Square :: Square( int s)
Square( int s ) {
{ side = s; } side = s;
... }
};
Amity School of Engineering & Technology

Types of Constructors

• Default Constructor

• Parameterized Constructor

• Copy Constructor
Amity School of Engineering & Technology

Types of Constructors
• Three Types of Constructor
Amity School of Engineering & Technology

The Default Constructor


• Constructors can have any number of parameters, including none
• A default constructor is one that takes no arguments either due to
– No parameters or
– All parameters have default values
• Default constructor called automatically when the object is created.
Amity School of Engineering & Technology

Example:

class X
{
public:
X( ); // Default constructor with no arguments
X (int = 0); // Default constructor with one default argument
};
Default Constructors Amity School of Engineering & Technology

 Default constructor is the constructor which doesn’t take any argument.


 It has no parameters.
#include <iostream>
using namespace std; Output
class construct { a: 10
public: b: 20
int a, b;
// Default Constructor
construct()
{ a = 10;
b = 20; } };
int main()
{ // Default constructor called automatically when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 1; }
Amity School of Engineering & Technology

The Parameterized Constructor


• Constructors should have any number of parameters,
• A Parameterized constructor is one that takes arguments.
Parameterized Constructors
Amity School of Engineering & Technology

 Parameterized Constructor helps you to assign initial value to an object at the time of its
creation

#include <iostream> int getY()


Output
using namespace std; { return y; } }; p1.x = 10,
class Point { int main() p1.y = 15
private: {
int x, y; // Constructor called
public: Point p1(10, 15);
// Parameterized Constructor // Access values assigned by
Point(int x1, int y1) constructor
{ x = x1; cout << "p1.x = " <<
y = y1; } p1.getX() << ", p1.y = " << p1.getY();
int getX() return 0;
{ return x; } }
Amity School of Engineering & Technology

Copy Constructor
• Constructor that is used to initialize and declare an object from another
object is known as a copy constructor in C++.

33
Copy Constructors Amity School of Engineering & Technology

#include<iostream> int main()


using namespace std; {
class Point Point p1(10, 15); // Normal constructor Point p2
{ = p1; // Copy constructors // values assigned
private: by constructors
int x, y; cout << "p1.x = " << p1.getX() << ", p1.y = " <<
public: p1.getY();
Point(int x1, int y1) { x = x1; y = y1; } cout << "\np2.x = " << p2.getX() << ", p2.y = "
// Copy constructor << p2.getY();
Point(Point &p2) {x = p2.x; y = p2.y; } return 0;
int getX() { return x; } }
int getY() { return y; }
};
Output
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
Amity School of Engineering & Technology

Defining copy constructors is very important


• In the absence of a copy constructor, the C++ compiler builds
a default copy constructor for each class which is doing a
member wise copy between objects.
Amity School of Engineering & Technology

SPECIAL CHARACTERISTICS OF CONSTRUCTORS


• These are called automatically when the objects are created.
• All objects of the class having a constructor are initialized before some use.
• These should be declared in the public section for availability to all the functions.
• Return type (not even void) cannot be specified for constructors.
• These cannot be inherited, but a derived class can call the base class constructor.
• These can have default arguments as other C++ functions.
• A constructor can call member functions of its class.
• An object of a class with a constructor cannot be used as a member of a union.
• We can use a constructor to create new objects .
• The make implicit calls to the memory allocation and deallocation operators new and
delete.
• These cannot be virtual.
Destructors Amity School of Engineering & Technology

• Deletes an object.
• A destructor function is called automatically when the object goes out of scope:
(1) the function ends
(2) the program ends
(3) a block containing temporary variables ends
(4) a delete operator is called
• A destructor has:
(i) the same name as the class but is preceded by a tilde (~)
(ii) no arguments and return no values
(iii) Only 1 destructor is allowed per class (i.e., it cannot be overloaded)

• Destructor called automatically at end of program in reverse order of their


appearance in the class definition
Amity School of Engineering & Technology

Destructors
using namespace std;
Example
class Demo {
private: int num1, num2;
public: Demo(int n1, int n2)
{ cout<<"Inside Constructor"<<endl;
num1 = n1; num2 = n2; }
void display()
{ cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl; }
~Demo() { cout<<"Inside Destructor"; }
};
int main() { Demo obj1(10, 20); obj1.display(); return 0; }
Local Class and GlobalAmity
ClassSchool of Engineering & Technology

Local Class: #include<iostream>


using namespace std;
A class declared inside a function
becomes local to that function and is void fun()
called Local Class in C++. {
For example, in the program, Test is a class Test // local to fun
local class in fun(). {
All the methods of Local classes must /* members of Test class */
be defined inside the class only.
};
A Local class cannot contain static data
}
members. It may contain static functions
though. int main()
Local classes can access global types, {
variables and functions. return 0;
}
Local Class and Global Class
Amity School of Engineering & Technology

Global Class: #include<iostream>


A class defined outside all using namespace std;
methods is a global class because class Test // Global class
its objects can be created from {
anywhere in the program. void fun()
{
}
};
int main()
{
return 0;
}
1) A local class type name can only be used in the enclosing function
Amity School of Engineering & Technology

#include<iostream> int main()


using namespace std; {
void fun()
Test t; // Error
{
// Local class Test *tp; // Error
class Test return 0;
{ }
/* ... */
};
Test t; // Fine
Test *tp; // Fine
}
2) All the methods of Local classes must be defined inside the class only
Amity School of Engineering & Technology

#include<iostream> int main()


using namespace std; {
void fun() fun();
{ class Test // local to fun return 0;
{ public: }
// Fine as the method is defined inside the local Output:
class Local Class method() called
void method() {
cout << "Local Class method() called";
}
};
Test t;
t.method();
}
All the methods of Local classes must be defined inside the class only
Amity School of Engineering & Technology

#include<iostream> // Error as the method is defined outside the local class


using namespace std; void Test::method()
void fun() {
{ cout << "Local Class method()";
class Test // local to fun }
{ }
public: int main()
void method(); {
}; return 0;
}
Compiler Error:
In function 'void fun()': error: a function-definition is not
allowed here before '{' token
3. Local classes can access global types, variables and functions
Amity School of Engineering & Technology

#include<iostream> Test1 t1;


using namespace std; public:
int x=0; void method() {
void fun() // Fine: Local class member methods can access
{ global variables.
// First Local class cout << "x = " << x << endl;
class Test1 { }
public: };
Test1() { cout << "Test1::Test1()" << endl; } Test2 t;
}; t.method();
// Second Local class }
class Test2 int main()
{ {
// Fine: A local class can use other local fun(); return 0;
classes of same function }
Output Test1::Test1()
x=0
Amity School of Engineering & Technology

Some interesting facts about local classes.


• A local class type name can only be used in the enclosing function.

• All the methods of Local classes must be defined inside the class only

• A Local class cannot contain static data members. It may contain static
functions though.

• Local classes can access global types, variables and functions. Also, local
classes can access other local classes of same function..
Amity School of Engineering & Technology

Thank You

You might also like