CLASS AND
OBJECTS
Presented by:
Mohit Kumar(24hel2129)
Abhishek
Yadav(24hel2104)
Aryan Yadav(24hel2111)
Ayush
CLASS
• a class is a blueprint for creating objects. It defines the properties (data members) and
behaviors (member functions) that the objects created from the class will have. Classes are
central to object-oriented programming in C++, enabling encapsulation, inheritance, and
polymorphism.
• Specifying a class:
Class class_name
{
Private:
Variable declarations;
Function declarations;
Public:
Variable declarations;
Function declaration;
}
A class
example
Outside function inline
One of the objectives of OOP is to separate the details of implementation from the class definition, It is therefore good practice to define
the member functions outside the class #include <iostream>
#include <iostream>
// Function declaration inside the class
class MyClass {
public:
// Inline function declaration
inline void greet();
};
// Function definition outside the class
inline void MyClass::greet() {
std::cout << "Hello from MyClass!" << std::endl;
}
int main() {
MyClass obj;
obj.greet();
return 0;
}
Nesting of member functions
member function of a class can be called only by an object of that class using a dot operator.
However, there is an exception to this. A member function can be called by using its name inside
another member function of the same class. This is known as nesting of member functions.
Private member function
• A private member functions are functions that are declared inside a class and are only accessible within
that class. They are not accessible from outside the class, not even by objects of the same class.
class BankAccount {
private:
double balance;
void updateBalance(double amount) { // Private member function
balance += amount;
}
public:
void deposit(double amount) {
updateBalance(amount);
}
void withdraw(double amount) {
updateBalance(-amount);
}
};
Arrays within a class
• The arrays can be used as member variables in a class. The following class definition is valid.
• Const int size= 10;
• class array
• {
• int a[size]: // ‘a’ is int type array
• public:
• void setval (void);
• void display(void);
• }
STATIC DATA MEMBERS WITH EXAMPLE
STATIC DATA MEMBERS ARE SHARED AMONG ALL OBJECTS OF A CLASS. THEY BELONG TO THE CLASS RATHER THAN ANY SPECIFIC OBJECT.
Arrays of Objects
An array of objects is an array where each element is an object of a class. This allows you to
store multiple objects of the same type in a single array.
Objects as
function
arguments
1. Pass by Value: A copy of
the object is passed to
the function or method.
2. Pass by Reference: A
reference to the original
object is passed to the
function or method.
3. Pass by Constant
Reference: A constant
reference to the original
object is passed to the
function or method.
UNIT – 3
OBJECT ORIENTED PROGRAMMING
Chapter 6 – Constructors and Destructors
Constructors in C++
A constructor is a member function of a class which initializes objects
of a class. In C++, Constructor is automatically called when object
(instance of class) is created. It is special member function of the class.
 A constructor is different from normal functions in following ways:
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object is created.
• If we do not specify a constructor, C++ compiler generates a default
constructor for us (expects no parameters and has an empty body).
Types of Constructors
Default Constructors: Default constructor is the constructor which
doesn’t take any argument. It has no parameters.
• C++ program to illustrate the concept of Constructors
#include <iostream>
using namespace std;
class construct
{
public:
int a, b;
construct() // Default Constructor
{
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;
}
 Output:
a: 10 b: 20
Parameterized Constructors:
It is possible to pass arguments to constructors. Typically, these
arguments help initialize an object when it is created. To create a
parameterized constructor, simply add parameters to it the way
you would to any other function. When you define the
constructor’s body, use the parameters to initialize the object.
• C++ program to illustrate parameterized constructors
#include <iostream>
using namespace std;
class Point
{
private:
Int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
return 0;
}
 Output:
p1.x = 10, p1.y = 15
Uses of Parameterized constructor:
– It is used to initialize the various data elements of
different objects with different values when they are
created.
– It is used to overload constructors.
Can we have more than one constructors in a class?
Yes, It is called Constructor Overloading.
Copy Constructor: A copy constructor is a member
function which initializes an object using another object of
the same class.
INHERITANCE
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and
behavior from another class. The class that is being inherited from is called the base class or parent class, while the class that
is doing the inheriting is called the derived class or child class.
• In C++, inheritance is denoted by a colon followed by the name of the base class. For example:
• Class Animal {
• public:
• void sound() { std::cout << “Animal makes a sound” << std::endl; }
• };
•
• class Dog : public Animal {
• public:
• void sound() { std::cout << “Dog barks” << std::endl; }
};
SINGLE INHERITANCE
• Single inheritance in C++ is when a class inherits from a single base class. The syntax for single inheritance is:
• Class DerivedClass : public BaseClass {
• // members of DerivedClass
};
Example
Class Animal {
public:
void sound() { std::cout << “Animal makes a sound” << std::endl; }
};
class Dog : public Animal {
public:
void bark() { std::cout << “Dog barks” << std::endl; }
};
MULTILEVEL INHERITANCE
Multilevel inheritance is a type of inheritance in C++ where a class inherits from a base class, and the base class itself inherits from another base class.
This creates a hierarchy of classes, where a class can inherit properties and behavior from multiple levels of base classes.
Syntax:class Animal {
public:
void sound() { std::cout << “Animal makes a sound” << std::endl; }
};
class Mammal : public Animal {
public:
void eat() { std::cout << “Mammal eats” << std::endl; }
};
class Dog : public Mammal {
public:
void bark() { std::cout << “Dog barks” << std::endl; }
};
MULTIPLE INHERITANCE
Multiple inheritance is a type of inheritance in C++ where a class can inherit properties and behavior from multiple base classes. This means that a class can have
more than one direct parent class.
Syntax:class BaseClass1 {
// members of BaseClass1
};
class BaseClass2 {
// members of BaseClass2
};
class DerivedClass : public BaseClass1, public BaseClass2 {
// members of DerivedClass
};
HIERIECAL INHERITANCE
• Hierarchical inheritance is a type of inheritance in C++ where a class inherits from a base class, and another class inherits from the first class, creating a hierarchy of
classes. This allows for a more organized and structured way of defining classes, where a class can inherit properties and behavior from multiple levels of base classes.
• Ex:-class Animal {
• public:
• void sound() { std::cout << “Animal makes a sound” << std::endl; }
• };
• class Mammal : public Animal {
• public:
• void eat() { std::cout << “Mammal eats” << std::endl; }
• };
• class Carnivore : public Mammal {
• public:
• void hunt() { std::cout << “Carnivore hunts” << std::endl; }
• };
• class Lion : public Carnivore {
• public:
• void roar() { std::cout << “Lion roars” << std::endl; }
};
UNIT – 3
OBJECT ORIENTED PROGRAMMING
CHAPTER – 12 : TEMPLATES
Template is a concept which enables us to define generic classes and
functions and thus provides support for generic programming. Generic
programming is an approach where generic types are used as
parameters in algorithm so that they work for a variety of suitable
data types and data structures.
A template can be considered as a kind of macro. When an object of a
specific type is defined for actual use. The template definition for
that class is substituted with the required data type. Since a template
is defined with a parameter that would be replaced by a specified
data type at the time of actual use of the class or function, the
templates are sometimes called parameterized classes or functions.
CLASS TEMPLATES
The class template definition is very similar to an ordinary class definition except the prefix
template<class T> and the use of type T. This prefix tells the compiler that we are going m declare a
template and use T as a type name in the declaration. Thus, vector has become a parameterized class
with the type T as its parameter. T may be substituted by any data type including the user-defined
types. Now, we can create vectors for holding different data types.
• Example :
vector <int> v1(1O); // 10 element int vector
vector <float> v2(25); // 25 element float vector
 Syntax of a Class Template :
template<class T>
class vector
{
T* Vi; // Type T vector
int size ;
public:
vector(int m)
{
v = new T[size = m] ;
for(int i = 0; i < size; i++)
v [i] = 0;
}
vector (T* a)
{
for (int i=0; i<size; i ++)
v[i] = a[i];
}
T operator* (vector &y)
{
T sum = 0;
for(int i = 0; i < size; i++)
sum+= this-> v [i) * y . v [ i ];
return sum;
}
}
FUNCTION TEMPLATES
Like class templates, we can also define function templates that could be used to
create a family of functions with different argument types.
 The function template syntax is similar to that of the class template except that we
are defining functions instead of classes. We must use the template parameter T as
an when necessary in the function body and in its argument list.
• The following example declares a swap() function template that will swap two
values of a given type if data.
template<class T>
void swap(T&x, T&y)
{
T temp= x;
x = y;
y = temp;
}

C++ Presen. tation.pptx

  • 1.
    CLASS AND OBJECTS Presented by: MohitKumar(24hel2129) Abhishek Yadav(24hel2104) Aryan Yadav(24hel2111) Ayush
  • 2.
    CLASS • a classis a blueprint for creating objects. It defines the properties (data members) and behaviors (member functions) that the objects created from the class will have. Classes are central to object-oriented programming in C++, enabling encapsulation, inheritance, and polymorphism. • Specifying a class: Class class_name { Private: Variable declarations; Function declarations; Public: Variable declarations; Function declaration; }
  • 3.
  • 4.
    Outside function inline Oneof the objectives of OOP is to separate the details of implementation from the class definition, It is therefore good practice to define the member functions outside the class #include <iostream> #include <iostream> // Function declaration inside the class class MyClass { public: // Inline function declaration inline void greet(); }; // Function definition outside the class inline void MyClass::greet() { std::cout << "Hello from MyClass!" << std::endl; } int main() { MyClass obj; obj.greet(); return 0; }
  • 5.
    Nesting of memberfunctions member function of a class can be called only by an object of that class using a dot operator. However, there is an exception to this. A member function can be called by using its name inside another member function of the same class. This is known as nesting of member functions.
  • 6.
    Private member function •A private member functions are functions that are declared inside a class and are only accessible within that class. They are not accessible from outside the class, not even by objects of the same class. class BankAccount { private: double balance; void updateBalance(double amount) { // Private member function balance += amount; } public: void deposit(double amount) { updateBalance(amount); } void withdraw(double amount) { updateBalance(-amount); } };
  • 7.
    Arrays within aclass • The arrays can be used as member variables in a class. The following class definition is valid. • Const int size= 10; • class array • { • int a[size]: // ‘a’ is int type array • public: • void setval (void); • void display(void); • }
  • 8.
    STATIC DATA MEMBERSWITH EXAMPLE STATIC DATA MEMBERS ARE SHARED AMONG ALL OBJECTS OF A CLASS. THEY BELONG TO THE CLASS RATHER THAN ANY SPECIFIC OBJECT.
  • 9.
    Arrays of Objects Anarray of objects is an array where each element is an object of a class. This allows you to store multiple objects of the same type in a single array.
  • 10.
    Objects as function arguments 1. Passby Value: A copy of the object is passed to the function or method. 2. Pass by Reference: A reference to the original object is passed to the function or method. 3. Pass by Constant Reference: A constant reference to the original object is passed to the function or method.
  • 11.
    UNIT – 3 OBJECTORIENTED PROGRAMMING Chapter 6 – Constructors and Destructors
  • 12.
    Constructors in C++ Aconstructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object (instance of class) is created. It is special member function of the class.  A constructor is different from normal functions in following ways: • Constructor has same name as the class itself • Constructors don’t have return type • A constructor is automatically called when an object is created. • If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).
  • 13.
    Types of Constructors DefaultConstructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters. • C++ program to illustrate the concept of Constructors #include <iostream> using namespace std; class construct { public: int a, b;
  • 14.
    construct() // DefaultConstructor { 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; }  Output: a: 10 b: 20
  • 15.
    Parameterized Constructors: It ispossible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object. • C++ program to illustrate parameterized constructors #include <iostream> using namespace std; class Point
  • 16.
    { private: Int x, y; public: //Parameterized Constructor Point(int x1, int y1) { x = x1; y = y1; } int getX() { return x; } int getY()
  • 17.
    { return y; } };int main() { //Constructor called Point p1(10, 15); // Access values assigned by constructor cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); return 0; }  Output: p1.x = 10, p1.y = 15
  • 18.
    Uses of Parameterizedconstructor: – It is used to initialize the various data elements of different objects with different values when they are created. – It is used to overload constructors. Can we have more than one constructors in a class? Yes, It is called Constructor Overloading. Copy Constructor: A copy constructor is a member function which initializes an object using another object of the same class.
  • 19.
    INHERITANCE Inheritance is afundamental concept in object-oriented programming (OOP) that allows one class to inherit properties and behavior from another class. The class that is being inherited from is called the base class or parent class, while the class that is doing the inheriting is called the derived class or child class. • In C++, inheritance is denoted by a colon followed by the name of the base class. For example: • Class Animal { • public: • void sound() { std::cout << “Animal makes a sound” << std::endl; } • }; • • class Dog : public Animal { • public: • void sound() { std::cout << “Dog barks” << std::endl; } };
  • 20.
    SINGLE INHERITANCE • Singleinheritance in C++ is when a class inherits from a single base class. The syntax for single inheritance is: • Class DerivedClass : public BaseClass { • // members of DerivedClass }; Example Class Animal { public: void sound() { std::cout << “Animal makes a sound” << std::endl; } }; class Dog : public Animal { public: void bark() { std::cout << “Dog barks” << std::endl; } };
  • 21.
    MULTILEVEL INHERITANCE Multilevel inheritanceis a type of inheritance in C++ where a class inherits from a base class, and the base class itself inherits from another base class. This creates a hierarchy of classes, where a class can inherit properties and behavior from multiple levels of base classes. Syntax:class Animal { public: void sound() { std::cout << “Animal makes a sound” << std::endl; } }; class Mammal : public Animal { public: void eat() { std::cout << “Mammal eats” << std::endl; } }; class Dog : public Mammal { public: void bark() { std::cout << “Dog barks” << std::endl; } };
  • 22.
    MULTIPLE INHERITANCE Multiple inheritanceis a type of inheritance in C++ where a class can inherit properties and behavior from multiple base classes. This means that a class can have more than one direct parent class. Syntax:class BaseClass1 { // members of BaseClass1 }; class BaseClass2 { // members of BaseClass2 }; class DerivedClass : public BaseClass1, public BaseClass2 { // members of DerivedClass };
  • 23.
    HIERIECAL INHERITANCE • Hierarchicalinheritance is a type of inheritance in C++ where a class inherits from a base class, and another class inherits from the first class, creating a hierarchy of classes. This allows for a more organized and structured way of defining classes, where a class can inherit properties and behavior from multiple levels of base classes. • Ex:-class Animal { • public: • void sound() { std::cout << “Animal makes a sound” << std::endl; } • }; • class Mammal : public Animal { • public: • void eat() { std::cout << “Mammal eats” << std::endl; } • }; • class Carnivore : public Mammal { • public: • void hunt() { std::cout << “Carnivore hunts” << std::endl; } • }; • class Lion : public Carnivore { • public: • void roar() { std::cout << “Lion roars” << std::endl; } };
  • 24.
    UNIT – 3 OBJECTORIENTED PROGRAMMING CHAPTER – 12 : TEMPLATES Template is a concept which enables us to define generic classes and functions and thus provides support for generic programming. Generic programming is an approach where generic types are used as parameters in algorithm so that they work for a variety of suitable data types and data structures. A template can be considered as a kind of macro. When an object of a specific type is defined for actual use. The template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by a specified data type at the time of actual use of the class or function, the templates are sometimes called parameterized classes or functions.
  • 25.
    CLASS TEMPLATES The classtemplate definition is very similar to an ordinary class definition except the prefix template<class T> and the use of type T. This prefix tells the compiler that we are going m declare a template and use T as a type name in the declaration. Thus, vector has become a parameterized class with the type T as its parameter. T may be substituted by any data type including the user-defined types. Now, we can create vectors for holding different data types. • Example : vector <int> v1(1O); // 10 element int vector vector <float> v2(25); // 25 element float vector  Syntax of a Class Template : template<class T> class vector { T* Vi; // Type T vector int size ; public: vector(int m)
  • 26.
    { v = newT[size = m] ; for(int i = 0; i < size; i++) v [i] = 0; } vector (T* a) { for (int i=0; i<size; i ++) v[i] = a[i]; } T operator* (vector &y) { T sum = 0; for(int i = 0; i < size; i++) sum+= this-> v [i) * y . v [ i ]; return sum; } }
  • 27.
    FUNCTION TEMPLATES Like classtemplates, we can also define function templates that could be used to create a family of functions with different argument types.  The function template syntax is similar to that of the class template except that we are defining functions instead of classes. We must use the template parameter T as an when necessary in the function body and in its argument list. • The following example declares a swap() function template that will swap two values of a given type if data. template<class T> void swap(T&x, T&y) { T temp= x; x = y; y = temp; }