Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Arrays within a Class

•Arrays can be declared as the members of a class.


•The arrays can be declared as private, public or protected members of the class.
•To understand the concept of arrays as members of a class, consider this example.
•A program to demonstrate the concept of arrays as class members

www.bennett.edu.in
#include<iostream>
const int size=5; void student ::
class student { int tot_marks()
roll_no; int //calculating total
marks[size]; public: marks { int total=0;
void getdata (); void for(int i=0; i<size; i+
tot_marks (); } ; +) total+ = marks[i];
void student :: cout<<"\n\nTotal
getdata () { cout<<"\ marks "<<total; }
nEnter roll no: "; void main() student
Cin>>roll_no; stu; stu.getdata() ;
for(int i=0; i<size; i+ stu.tot_marks() ;
+) { cout<<"Enter getch(); }
marks in
subject"<<(i+1)<<":
"; cin>>marks[i] ; }

www.bennett.edu.in
Array of Objects
When a class is defined, only the specification for the object is defined; no memory or
storage is allocated. To use the data and access functions defined in the class, you need to
create objects.

ClassName ObjectName[number of objects];

The Array of Objects stores objects.

An array of a class type is also known as an array of objects.

www.bennett.edu.in
void Employee::getdata(){//Defining of function
cout<<"Enter Id : ";
// C++ program to implement
cin>>id;
// the above approach
cout<<"Enter Name : ";
#include<iostream>
cin>>name;
using namespace std;
}
void Employee::putdata(){//Defining of function
class Employee
cout<<id<<" ";
{
cout<<name<<" ";
int id;
cout<<endl;
char name[30];
}
public:
int main(){
void getdata();//Declaration of function
Employee emp; //One member
void putdata();//Declaration of function
emp.getdata();//Accessing the function
};
emp.putdata();//Accessing the function
return 0;

}
www.bennett.edu.in
Friend Class and Function in C++
A friend class can access private and protected members of other classes in which it is
declared as a friend. It is sometimes useful to allow a particular class to access private
and protected members of other classes.

For example, a LinkedList class may be allowed to access private members of Node.
We can declare a friend class in C++ by using the friend keyword.

Syntax:
friend class class_name; // declared in the base class

www.bennett.edu.in
// Here, class F is declared as a
// C++ Program to demonstrate the // friend inside class GFG. Therefore,
// functioning of a friend class // F is a friend of class GFG. Class F
#include <iostream> // can access the private members of
using namespace std; // class GFG.
class F {
class GFG { public:
private: void display(GFG& t)
int private_variable; {
cout << "The value of Private Variable = "
protected: << t.private_variable << endl;
int protected_variable; cout << "The value of Protected Variable = "
<< t.protected_variable;
public: }
GFG() };
{
private_variable = 10; // Driver code
protected_variable = 99; int main()
} {
GFG g;
// friend class declaration F fri;
friend class F; fri.display(g);
}; return 0;
}

www.bennett.edu.in
Constructors in C++
Constructor in C++ is a special method that is invoked automatically at the time
of object creation. It is used to initialize the data members of new objects generally.
The constructor in C++ has the same name as the class or structure. It constructs
the values i.e. provides data for the object which is why it is known as constructor.

• Constructor is a member function of a class, whose name is same as the class


name.

• Constructor is a special type of member function that is used to initialize the data
members for an object of a class automatically, when an object of the same class is
created.

www.bennett.edu.in
• Constructor is invoked at the time of object creation. It constructs the
values i.e. provides data for the object that is why it is known as
constructor.
• Constructor do not return value, hence they do not have a return type.

The prototype of the constructor looks like <class-name> (list-of-


parameters);

www.bennett.edu.in
Rules for operator overloading
In C++, following are the general rules for operator overloading.
1) Only built-in operators can be overloaded. New operators can not be created.
2) Arity of the operators cannot be changed.
3) Precedence and associativity of the operators cannot be changed.
4) Overloaded operators cannot have default arguments except the function call operator ()
which can have default arguments.
5) Operators cannot be overloaded for built in types only. At least one operand must be used
defined type.
6) Assignment (=), subscript ([]), function call (“()”), and member selection (->) operators must
be defined as member functions
7) Except the operators specified in point 6, all other operators can be either member functions
or a non member functions.
8 ) Some operators like (assignment)=, (address)& and comma (,) are by default overloaded.
www.bennett.edu.in
Operator Overloading in C++
Operator overloading is a compile-time polymorphism. It is an idea of giving special
meaning to an existing operator in C++ without changing its original meaning.

In C++, we can make operators work for user-defined classes. This means C++ has the
ability to provide the operators with a special meaning for a data type, this ability is
known as operator overloading. For example, we can overload an operator ‘+’ in a
class like String so that we can concatenate two strings by just using +. Other example
classes where arithmetic operators may be overloaded are Complex Numbers,
Fractional Numbers, Big integers, etc.

www.bennett.edu.in
Example:

int a; float b,sum; sum = a + b;

Here, variables “a” and “b” are of types “int” and “float”, which are
built-in data types. Hence the addition operator ‘+’ can easily add the
contents of “a” and “b”. This is because the addition operator “+” is
predefined to add variables of built-in data type only.

www.bennett.edu.in
Type Conversion in C++

A type cast is basically a conversion from one type to another. There are two types of
type conversion:

1.Implicit Type Conversion Also known as ‘automatic type conversion’.


1. Done by the compiler on its own, without any external trigger from the user.
2. Generally takes place when in an expression more than one data type is present.
In such condition type conversion (type promotion) takes place to avoid lose of
data.
3. All the data types of the variables are upgraded to the data type of the variable
with largest data type.

www.bennett.edu.in
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long double

•It is possible for implicit conversions to lose information, signs can be lost (when
signed is implicitly converted to unsigned), and overflow can occur (when long
long is implicitly converted to float).

www.bennett.edu.in
C++ Inheritance

In C++, inheritance is a process in which one object acquires all the


properties and behaviors of its parent object automatically. In such way,
you can reuse, extend or modify the attributes and behaviors which are
defined in other class.
In C++, the class which inherits the members of another class is called
derived class and the class whose members are inherited is called base
class. The derived class is the specialized class for the base class.

www.bennett.edu.in
Advantage of C++ Inheritance

Code reusability: Now you can reuse the members of your parent class. So, there is
no need to define the member again. So, less code is required in the class.

www.bennett.edu.in
Types Of Inheritance

C++ supports five types of inheritance:

•Single inheritance
•Multiple inheritance
•Hierarchical inheritance
•Multilevel inheritance
•Hybrid inheritance

www.bennett.edu.in
1. Single Inheritance: In single inheritance, a class is allowed to inherit from only
one class. i.e. one subclass is inherited by one base class only.
Syntax:

class subclass_name : access_mode base_class


{
// body of subclass };

OR

class A
{ ... .. ... };
class B: public A
{ ... .. ... };

www.bennett.edu.in
2. Multiple Inheritance is a feature of C++ where a class can inherit from more than
one classes. The constructors of inherited classes are called in the same order in which
they are inherited. For example, in the following program, B’s constructor is called
before A’s constructor.
A class can be derived from more than one base class.
Eg:
(i) A CHILD class is derived from FATHER and MOTHER class
(ii) A PETROL class is derived from LIQUID and FUEL class.

www.bennett.edu.in
In Hierarchical inheritance, more than one sub-class inherits the property
of a single base class. There is one base class and multiple derived
classes. Several other classes inherit the derived classes as well.
Hierarchical structures thus form a tree-like structure. It is similar to
that, mango and apple both are fruits; both inherit the property of fruit.
Fruit will be the Base class, and mango and apple are sub-classes.

www.bennett.edu.in
Multilevel Inheritance in C++ is the process of deriving a class from another
derived class. When one class inherits another class it is further inherited by
another class. It is known as multi-level inheritance.

For example, if we take Grandfather as a base class then Father is the derived class
that has features of Grandfather and then Child is the also derived class that is
derived from the sub-class Father which inherits all the features of Father.

Example:
Base class-> Wood, Intermediate class-> furniture, subclass-> table

www.bennett.edu.in
Hybrid Inheritance

Hybrid inheritance in C++ involves a combination of multiple


inheritance and single inheritance. It allows a class to inherit from
more than one base class, and those base classes can themselves
inherit from other classes. Here's an example of a C++ program that
demonstrates hybrid inheritance:

www.bennett.edu.in
#include <iostream> // Derived class 1 inheriting from
// Derived class 3 inheriting from both Dog and
Sparrow
Animal class HybridPet : public Dog, public Sparrow {
// Base class 1 class Dog : public Animal { public:
class Animal { public: void play() {
public: void bark() { std::cout << "HybridPet is playing." <<
void eat() { std::cout << "Dog is barking." << std::endl;
std::cout << "Animal is eating." <<
std::endl; }
std::endl; } };
} };
}; int main() {
HybridPet myPet;
// Derived class 2 inheriting from Bird
// Base class 2 class Sparrow : public Bird { myPet.eat(); // Accessing Animal's method
class Bird { public: myPet.fly(); // Accessing Bird's method
public: void chirp() { myPet.bark(); // Accessing Dog's method
void fly() { std::cout << "Sparrow is chirping." myPet.chirp(); // Accessing Sparrow's method
std::cout << "Bird is flying." << myPet.play(); // Accessing HybridPet's
<< std::endl; method
std::endl; }
} }; return 0;
}; }

www.bennett.edu.in
Runtime Polymorphism

Runtime polymorphism in C++ is achieved through virtual


functions and dynamic binding. It allows you to call a derived
class's function through a base class pointer or reference, and the
appropriate function is determined at runtime. Here's an example of
a C++ program that demonstrates runtime polymorphism:

www.bennett.edu.in
Thank You…

www.bennett.edu.in

You might also like