0% found this document useful (0 votes)
4 views21 pages

Programming

The document discusses various concepts in C++ programming, including operator overloading, type conversions, and inheritance. It explains how to convert between basic types and class types using constructors and conversion functions, as well as the different types of inheritance such as single, multilevel, multiple, hierarchical, and hybrid inheritance. Additionally, it covers the visibility modes in inheritance and how they affect member access in derived classes.

Uploaded by

n27murmu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Programming

The document discusses various concepts in C++ programming, including operator overloading, type conversions, and inheritance. It explains how to convert between basic types and class types using constructors and conversion functions, as well as the different types of inheritance such as single, multilevel, multiple, hierarchical, and hybrid inheritance. Additionally, it covers the visibility modes in inheritance and how they affect member access in derived classes.

Uploaded by

n27murmu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

#include<iostream.

h>
#include<conio.h>
class abc
{
public:
int x;
void getvalue()
{
cout<<"enter the length";
cin>>x;
}
public:

abc operator +(abc obj1)


{
abc obj2;
obj2.x=x+obj1.x;
return(obj2);
}
void display()
{
cout<<"the total length is"<<x;
}
};
void main()
{
clrscr();
abc a1,a2,a3;
a2.getvalue();
a3.getvalue();
a1=a2+a3;
a1.display();
getch();
}
TYPE CONVERSIONS
 When variables of different types are
mixed together in an expression, the
compiler applies the rules of automatic
type conversion.
 The assignment operator also causes
type conversions. The type of data on
the right is converted to the type of data
on the left
TYPE CONVERSIONS
 If both the operands are built in
datatypes, then also the operation
happens.
 But when one operand is a class and
another one is a built in type, there will
be error.
TYPE CONVERSIONS
 Built in to class: Using Constructors

 Class to built in: Using conversion


function or Casting operator

 Class to class: Either Constructors or


casting operators
BASIC TO CLASS TYPE
 Constructors can be used for type conversion from basic type to class type.
 #include<iostream.h>
 #include<conio.h>
 class abcd
 {
 public:
 int n;

 abcd(){}

 abcd(int x)
 {
 n=x;
 }
 void display()
 {
 cout<<"the value you have entered is"<<n;
 }
 };
 void main()
 {
 clrscr();
 int a;
 abcd obj;
 cout<<"enter a number";
 cin>>a;
 obj=a;
 obj.display();
 getch();
 }
CLASS TO BASIC TYPE

operator typename()
{
----
-----
}
An overloaded casting operator is used to
convert class to basic type
 #include<iostream.h>
 #include<conio.h>
 class c2b
 {
 public:
 float a;
 public:
 void getdata()
 {
 cout<<"enter a number";
 cin>>a;
 }
 operator float()
 {
 return a;
 }

 };
 void main()
 {
 clrscr();
 c2b x;
 float p;
 x.getdata();
 p=x;
 cout<<"the class is converted into an integer and the value is"<<p;
 getch();
 }
Class to basic
 Conversion function must be a class
member
 Must not specify a return type
 Must not have any arguments
One class to another class
 obj x=obj y;
 Class y type data is converted to class x
type and the value is assigned to x. y is
the source and x is the destination.
 Such convertions can be carried out by
a constructor or conversion function.
 #include<iostream.h>
 #include<conio.h>
 class cls2
 {
 public:
 int b;
 public:
 void getdata()
 {
 cout<<"enter a number";
 cin>>b;
 }
 };
 class cls1
 {
 public:
 int a;
 public:
 cls1()
 {}
 cls1(cls2 c2)
 {
 a=c2.b;
 }
 };

 void main()
 {
 clrscr();
 cls2 y;
 cls1 x;
 y.getdata();
 x=y;
 cout<<"the x class is converted into y class"<<x.a;
 getch();
 }
INHERITANCE
 stands for derivation.
 The mechanism of deriving a new class
from an old one is called inheritance.
 The old class is the base class and the
new one is called derived class.
 A derived class with only one base class
is called single inheritance
 A derived class with several base class
is multiple inheritance
INHERITANCE
 when more than one class is derived
from one base class, then it is
hierarchical inheritance
 Deriving a class from another derived
class is called multilevel inheritance
 Inheritance allows to reuse the class
that has been tested.
 It saves time and money and increases
reliability
FORMAT OF INHERITANCE
 class derivedclassname:visibilitymode
baseclassname
 class abc:private xyz
PRIVATE INHERITANCE
 When a base class is privately inherited
by a derived class, the ‘public’ members
of base class become private members
of the derived class and the public
members of the base class can be only
accessed by the member functions of
the derived class. No member of the
base class is accessible to the objects of
the derived class
PUBLIC INHERITANCE
 When the base class is publicly
inherited, ‘public’ members of the base
class become public members of the
derived class and therefore they are
accessible to the objects of the derived
class
INHERITANCE
 In both cases, private members of the base
class will never become members of its
derived class.
 A protected member can be accessed by
member function in its own class or in any
class derived from its own class but they
behave like private members to the rest of
the program
 In derived class, we can add member
variables and functions and extend the
functionality of the base class
SINGLE INHERITANCE
 A derived class from one base
class is called single inheritance.
MULTILEVEL INHERITANCE
 When a class is derived from
another derived class, it is called
multilevel inheritance.
MULTIPLE INHERITANCE
 When a class inherits the attributes from
two or more classes, it is known as
multiple inheritance. The base classes
are separated by commas (“,”)
A B C

D
HIERARCHIAL INHERITANCE
 When the features of one level are
shared by many others below that level.
 The base class will include all features
that are common to sub classes.
 The sub class can also serve as a base
class to another class
A

B C D

E F G H
HYBRID INHERITANCE
 A combination of different types of
inheritance.
A

C E

You might also like