Académique Documents
Professionnel Documents
Culture Documents
Rohit Sehgal
Nishit Majithia
June 2, 2016
With Structs you dont have the operations associated with data.
With Structs you dont have the operations associated with data.
e.g Consider a structure of complex numbers.
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.
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.
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
1 public
1 public
2 private
1 public
2 private
3 protected
1 public
2 private
3 protected
Generic programming
Generic programming
Type independent Programming.
Generic programming
Type independent Programming.
No need to specify the type for the data.
Generic programming
Type independent Programming.
No need to specify the type for the data.
sorting algorithm written for int will work for floats !
Example
template <typename X>
void max(X a,X b)
{
if(a>b)
return a;
else
return b
}
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.
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.
Bit Fields
Enumerations
Operator Overloading