Professional Documents
Culture Documents
Notes Module 2
Notes Module 2
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.
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 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.
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:
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:
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
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
•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:
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
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
www.bennett.edu.in
Thank You…
www.bennett.edu.in