Vous êtes sur la page 1sur 41

Introduction to C/C++

Lecture 9 - C++ Classes

Rohit Sehgal
Nishit Majithia

Association of Computing Activities,


Indian Institute of Technology,Kanpur
rsehgal@cse.iitk.ac.in
nishitm@cse.iitk.ac.in

June 2, 2016

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 1 / 16


Classes

With Structs you dont have the operations associated with data.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 2 / 16


Classes

With Structs you dont have the operations associated with data.
e.g Consider a structure of complex numbers.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 2 / 16


Classes

With Structs you dont have the operations associated with data.
e.g Consider a structure of complex numbers.
Have to create separate function to implement addition,subtraction.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 2 / 16


Classes

With Structs you dont have the operations associated with data.
e.g Consider a structure of complex numbers.
Have to create separate function to implement addition,subtraction.
These functions are accessible not only to these object but to every
other function or data.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 2 / 16


Classes Contd...

implements the idea of encapsulating functions with the data itself.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 3 / 16


Classes Contd...

implements the idea of encapsulating functions with the data itself.


Apart from this, classes allow the way to access the data within
object.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 3 / 16


Classes Contd...

implements the idea of encapsulating functions with the data itself.


Apart from this, classes allow the way to access the data within
object.
classes can give its capability to other class - Inheritance

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 3 / 16


Classes Contd...

implements the idea of encapsulating functions with the data itself.


Apart from this, classes allow the way to access the data within
object.
classes can give its capability to other class - Inheritance
Data can be processed in more than one form - Polymorphism

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 3 / 16


Encapsulation
Example
class complex
{
int real;
int img;

void print()
{
cout<<real<<"+i"<<img<<endl;
}
void add(complex c)
{
real += c.real;
img +=c.img;
}
};
Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 4 / 16
Encapsulation

Scope rules applies here as well.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 5 / 16


Encapsulation

Scope rules applies here as well.


member functions are not visible to outside world.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 5 / 16


Encapsulation

Scope rules applies here as well.


member functions are not visible to outside world.
can only be accessed by the object.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 5 / 16


Encapsulation

Scope rules applies here as well.


member functions are not visible to outside world.
can only be accessed by the object.
complex c1,c2;
c1.real=10;
c1.img=10;
c2.real=20;
c2.img=20;
c1.add(c2);
similar to structs !

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 5 / 16


Constructors
Data object Initialization upon creation.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 6 / 16


Constructors
Data object Initialization upon creation.
Constructors do not have return type, same name as that of class

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 6 / 16


Constructors
Data object Initialization upon creation.
Constructors do not have return type, same name as that of class
Example
class complex
{
int real;
int img;
complex(int a,int b)
{
real=a;
img=b;
}
};
...
complex c1(10,20);
Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 6 / 16
Poly-morphism

C++ allow functions to have same name,but header should be


different.
function overloading

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 7 / 16


Poly-morphism

C++ allow functions to have same name,but header should be


different.
function overloading
itself finds the best match during compilation.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 7 / 16


Poly-morphism

C++ allow functions to have same name,but header should be


different.
function overloading
itself finds the best match during compilation.
not only class member function but also all functions can be
overloaded.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 7 / 16


Polymorphism E.g

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 8 / 16


Access Specifier

1 public

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 9 / 16


Access Specifier

1 public
2 private

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 9 / 16


Access Specifier

1 public
2 private
3 protected

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 9 / 16


Access Specifier

1 public
2 private
3 protected

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 9 / 16


Inheritance

Ability of class to extend the capabilities from other class.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 10 / 16


Inheritance

Ability of class to extend the capabilities from other class.


Square is rectangle it has some properties similar to that of rectangle.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 10 / 16


Inheritance

Ability of class to extend the capabilities from other class.


Square is rectangle it has some properties similar to that of rectangle.
Same idea applies here.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 10 / 16


Inheritance

Ability of class to extend the capabilities from other class.


Square is rectangle it has some properties similar to that of rectangle.
Same idea applies here.
e.g Animals > Humans > Person > Male

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 10 / 16


Inheritance Example

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 11 / 16


C++ Templates

Generic programming

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 12 / 16


C++ Templates

Generic programming
Type independent Programming.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 12 / 16


C++ Templates

Generic programming
Type independent Programming.
No need to specify the type for the data.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 12 / 16


C++ Templates

Generic programming
Type independent Programming.
No need to specify the type for the data.
sorting algorithm written for int will work for floats !

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 12 / 16


Function Template

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 13 / 16


Function Template

Example
template <typename X>
void max(X a,X b)
{
if(a>b)
return a;
else
return b
}

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 13 / 16


Function Template

Example
template <typename X>
void max(X a,X b)
{
if(a>b)
return a;
else
return b
}

The operator used for the template type must be applicable to type of
actual argument, else error is raised.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 13 / 16


Function Template

Example
template <typename X>
void max(X a,X b)
{
if(a>b)
return a;
else
return b
}

The operator used for the template type must be applicable to type of
actual argument, else error is raised.
e.g structure dont support > so error will be raised by compiler.

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 13 / 16


Class Template
Example
template <class X>
class complex
{
X real;
X img;
...
void add(complex c)
{
real += c.real;
img +=c.img;
}
};
...
complex c1(10,20);
complex c2(1.1,2.3);
Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 14 / 16
Whats Next

Bit Fields
Enumerations
Operator Overloading

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 15 / 16


The End

Rohit Sehgal Nishit Majithia (IITK) ACA June 2, 2016 16 / 16

Vous aimerez peut-être aussi