0% found this document useful (0 votes)
25 views44 pages

Original PDF

1. A class defines the form of an object by combining data representation and methods to manipulate that data. Data and functions within a class are called members. 2. A class definition uses the class keyword followed by the class name and a class body within curly braces. Objects are then declared from the class. 3. Member functions allow objects to access and modify member data using the dot operator. Access can be public, private, or protected depending on where the member is declared within the class.

Uploaded by

arpitbhainader
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)
25 views44 pages

Original PDF

1. A class defines the form of an object by combining data representation and methods to manipulate that data. Data and functions within a class are called members. 2. A class definition uses the class keyword followed by the class name and a class body within curly braces. Objects are then declared from the class. 3. Member functions allow objects to access and modify member data using the dot operator. Access can be public, private, or protected depending on where the member is declared within the class.

Uploaded by

arpitbhainader
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/ 44

Classes and object

Class
• A class is used to specify the form of an object
and it combines data representation and
methods for manipulating that data into one
neat package.
• The data and functions within a class are
called members of the class.

55
C++ Class definitions
• A class definition starts with the
keyword class followed by the class name;
and the class body, enclosed by a pair of curly
braces. A class definition must be followed
either by a semicolon or a list of declarations.
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
56
Define C++ Objects
• A class provides the blueprints for objects, so
basically an object is created from a class. We
declare objects of a class with exactly the
same sort of declaration that we declare
variables of basic types.

Box Box1; // Declare Box1 of type Box

Box Box2; // Declare Box2 of type Box

57
Accessing data members
• The public data members of objects of a class
can be accessed using the direct member
access operator (.)
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;

// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0; 58
Defining member functions
• A member function of a class is a function
that has its definition within the class or
separately using scope resolution operator, ::

double Box::getVolume(void)
{
return length * breadth * height;
}

59
Calling a member functions
• A member function will be called using a dot
operator (.)

Box myBox; // Create an object


myBox.getVolume();

60
Example 1
// Member functions definitions
class Box void Box::setLength( double len )
{
{ length = len;
public: }
double length;
double breadth; void Box::setBreadth( double bre )
double height; {
breadth = bre;
void setLength( double len ); }
void setBreadth( double bre );
void setHeight( double hei ); void Box::setHeight( double hei )
{
double getVolume(void); height = hei;
}; }

double Box::getVolume(void)
{
return length * breadth * height;
}
61
Example 1
// Main function for the program
int main( )
{
Box Box1;
double volume = 0.0;

// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

// volume of box 1
volume = Box1.getVolume();
cout << volume <<endl;

return 0;
}

62
Example 2
class Box

Nesting of Member Functions


{
public:
double length;
double breadth;
double height;

double getVolume(void);

void setLength( double len );

void setBreadth( double bre );

void setHeight( double hei );


};

63
Example 2
// Member functions definitions

class Box double Box::getVolume(void)


{

Nesting of Member Functions


{ return length * breadth * height;
public: }
double length;
double breadth; void Box::setLength( double len )
double height; {
length = len;
}
void setLength( double len );
void setBreadth( double bre ); void Box::setBreadth( double bre )
void setHeight( double hei ); {
breadth = bre;
double getVolume(void); }
};
void Box::setHeight( double hei )
{
height = hei;
getVolume();
} 64
Example 2

// Main function for the program

Nesting of Member Functions


int main( )
{
Box Box1;
double volume = 0.0;

// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);

return 0;
}

65
C++ access modifiers
• The access restriction to the class members is specified
by the labeled public, private, and protected sections
within the class body.

class Base {
public:
// public members go here

protected:
// protected members go here

private:
// private members go here
}; 66
• Public : A public member is accessible from
anywhere outside the class but within a
program
class Line
{
public:
double length;
void setLength( double len );
double getLength( void );
};

// Member functions definitions


double Line::getLength(void)
{
return length ;
}
void Line::setLength( double len )
{
length = len;
}
67
• Public : A public member is accessible from
anywhere outside the class but within a
program

int main( )
{
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;

// set line length without member function


line.length = 10.0; // OK: because length is public
cout << "Length of line : " << line.length <<endl;
return 0;
}

68
• Private : A private member variable or function
cannot be accessed, or even viewed from
outside the class. Only the class and friend
functions can access private members.

class Line int main( )


{ {
private: Line line;
double length; line.length=20.0; // wrong
public: line.setLength(20.0); // Use member
function to set it.
void setLength( double l );
return 0;
};
}
void Box::setLength( double l )
{
length= l;
}

69
• Protected: A protected member variable or function is
very similar to a private member but it provided one
additional benefit that they can be accessed in child
classes which are called derived classes.
class Box
{
protected:
double width;
};

class SmallBox:Box //derived class.


{
public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};

// Member functions of child class


double SmallBox::getSmallWidth(void)
{
return width ;
}

70
• Protected: A protected member variable or function is
very similar to a private member but it provided one
additional benefit that they can be accessed in child
classes which are called derived classes.
class Box
{ void SmallBox::setSmallWidth( double wid )
protected: {
double width; width = wid;
}; }

class SmallBox:Box //derived class. int main( )


{ {
public: SmallBox box;
void setSmallWidth( double wid );
double getSmallWidth( void ); // set box width using member function
}; box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth()
// Member functions of child class << endl;
double SmallBox::getSmallWidth(void)
{ return 0;
return width ; }
}

71
Arrays within the class
• Arrays can be used as member variables in a
class.

class array
{
int regno[10];
}

72
ARRAYS OF OBJECTS
class Details void main()
{ {
private: Details det[50];
int salary; int n=0;
float ID; char ans;
public:
void getname( ) do{
{ cout << "Enter the Employee Number::" <<
cout << "\n Enter the Salary:"; n+1;
cin >> salary; det[n++].getname;
cout << "\n Enter the ID:"; cout << "Enter another (y/n)?: " ;
cin >> ID; cin >> ans;
} while ( ans != 'n' );
}
for (int j=0; j<n; j++)
void putname( )
{
{
cout << "\n Employee Number is:: " << j+1;
cout << "Employees" << salary << det[j].putname( );
"and ID is" << ID << '\n'; }
} }
}; 73
Passing and Returning Object from Function in C++
class Complex
{
private:
int real=0, imag=0;
public:
void Read()
{
cout<<"Enter real and imaginary
no"<<endl;
cin>>real>>imag;
}
void Add(Complex comp1,Complex comp2)
{
real=comp1.real+comp2.real;
imag=comp1.imag+comp2.imag;
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
74
Passing and Returning Object from Function in C++
class Complex
{
private: int main()
int real=0, imag=0; {
public: Complex c1,c2,c3;
void Read() c1.Read();
{ c2.Read();
cout<<"Enter real and imaginary no“ ; c3.Add(c1,c2);
cin>>real>>imag; c3.Display();
} return 0;
void Add(Complex comp1, Complex comp2) }
{
real=comp1.real+comp2.real;
imag=comp1.imag+comp2.imag;
}
void Display()
{
cout<<"Sum="<<real<<"+"<<imag<<"i";
}
};
75
Constructors &
Destructors
Constructor

• A class constructor is a special member function of


a class that is executed whenever we create new
objects of that class.
• A constructor will have exact same name as the
class
• It does not have any return type at all
• Constructors can be very useful for setting initial
values for certain member variables

77
EXAMPLE
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor

private:
double length;
};

78
EXAMPLE
Line::Line()
class Line {
{ cout << "Object is being created”;
public: }
void setLength( double len );
double getLength( void ); void Line::setLength( double len )
Line(); // This is the constructor {
length = len;
private: }
double length;
}; double Line::getLength( void )
{
return length;
}

79
EXAMPLE
Line::Line()
class Line {
{ cout << "Object is being created”;
public: }
void setLength( double len );
double getLength( void ); void Line::setLength( double len )
Line(); // This is the constructor {
length = len;
private: }
double length;
}; double Line::getLength( void )
{
int main( ) return length;
{ }
Line line;
// set line length
line.setLength(6.0);
cout << line.getLength() <<endl;

return 0;
} 80
Parameterized Constructor:

• A default constructor does not have any parameter,


but if you need, a constructor can have parameters.
This helps you to assign initial value to an object at
the time of its creation

81
EXAMPLE 1
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor

private:
double length;
};

Line::Line( double len)


{
cout << "Object is being created, length = " << len;
length = len;
}

82
EXAMPLE 1
void Line::setLength( double len )
{
class Line length = len;
{ }
public:
void setLength( double len ); double Line::getLength( void )
double getLength( void ); {
Line(double len); // This is the constructor return length;
}
private:
double length;
}; int main( )
{
Line line(10.0);

// get initially set length.


Line::Line( double len) cout << "Length of line : " << line.getLength();
{
cout << "Object is being created, length = " << len; // set line length again
length = len; line.setLength(6.0);
} cout << "Length of line : " << line.getLength();

return 0;
}
83
EXAMPLE 2
Multiple Constructors
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor1
Line(double len, double wid); // This is the c2

private:
double length, width;
};

Line::Line( double len)


{
cout << "Object is being created, length = " <<
len;
length = len;
}

84
EXAMPLE 2
Multiple Constructors
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor1
Line(double len, double wid); // This is the c2

private:
double length, width;
};

Line::Line( double len)


{
cout << "Object is being created, length = " <<
len;
length = len;
}

Line::Line( double len, double wid)


{
cout << "Object is being created ";
length = len; width= wid;
} 85
EXAMPLE 2
Multiple Constructors void Line::setLength( double len )
{
class Line length = len;
{
}
public:
void setLength( double len );
double getLength( void );
double Line::getLength( void )
Line(double len); // This is the constructor1 {
Line(double len, double wid); // This is the c2 return length;
}
private:
double length, width;
};

Line::Line( double len)


{
cout << "Object is being created, length = " <<
len;
length = len;
}

Line::Line( double len, double wid)


{
cout << "Object is being created ";
length = len; width= wid;
} 86
EXAMPLE 2
Multiple Constructors void Line::setLength( double len )
{
class Line length = len;
{
}
public:
void setLength( double len );
double getLength( void );
double Line::getLength( void )
Line(double len); // This is the constructor1 {
Line(double len, double wid); // This is the c2 return length;
}
private:
double length, width;
}; int main( )
{
Line::Line( double len) Line line(10.0);
{ Line line2(20.0,20.0);
cout << "Object is being created, length = " <<
len; // get initially set length.
length = len; cout << "Length of line : " << line2.getLength();
}
// set line length again
line.setLength(6.0);
Line::Line( double len, double wid)
cout << "Length of line : " << line2.getLength();
{
cout << "Object is being created ";
return 0;
length = len; width= wid;
}
} 87
Constructor with default arguments
class Area
{
private:
int length;
int breadth;

public:
Area()
{
length=5; breadth=5;
}

void GetLength()
{
cout<<"Enter length and breadth respectively: ";
cin>>length>>breadth;
}
int AreaCalculation()
{
return (length*breadth);
}

void DisplayArea(int temp)


{
cout<<"Area: "<<temp;
} 88
};
Constructor with default arguments
class Area
{
private:
int length; int main()
int breadth; {
Area A1,A2;
public: int temp;
Area()
{ A1.GetLength();
length=5; breadth=5; temp=A1.AreaCalculation();
}
A1.DisplayArea(temp);
void GetLength()
{ cout<<"Default Area when value is
cout<<"Enter length and breadth respectively: "; not taken from user"<<endl;
cin>>length>>breadth;
} temp=A2.AreaCalculation();
A2.DisplayArea(temp);
int AreaCalculation()
{ return 0;
return (length*breadth); }
}

void DisplayArea(int temp)


{
cout<<"Area: "<<temp;
} 89
};
Constructor Overloading
class Area
{
private:
int length;
int breadth;

public:
Area() { length=5; breadth=5; }
Area(int l, int b): length(l), breadth(b){ }

void GetLength()
{
cout<<"Enter length and breadth : ";
cin>>length>>breadth;
}
int AreaCalculation()
{
return (length*breadth);
}

void DisplayArea(int temp)


{
cout<<"Area: "<<temp<<endl;
}
}; 90
Constructor Overloading
class Area
{
private:
int length; int main()
int breadth; {
Area A1,A2(2,1);
public: int temp;
Area() { length=5; breadth=5; } cout<<"Default Area when no argument is passed.";
Area(int l, int b): length(l), breadth(b){ }
temp=A1.AreaCalculation();
void GetLength()
{ A1.DisplayArea(temp);
cout<<"Enter length and breadth : "; cout<<"Area when (2,1) is passed as argument.";
cin>>length>>breadth;
} temp=A2.AreaCalculation();
int AreaCalculation()
{ A2.DisplayArea(temp);
return (length*breadth);
} return 0;
}
void DisplayArea(int temp)
{
cout<<"Area: "<<temp<<endl;
}
}; 91
Dynamic initialization of objects

class Area
{
private:
int length;
int breadth;

public:

Area(int i, int j)
{
length=i; breadth=j;
}

int AreaCalculation()
{ return (length*breadth); }

void DisplayArea(int temp)


{
cout<<"Area: "<<temp;
}
}; 92
Dynamic initialization of objects

class Area
{
private:
int main()
int length;
{
int breadth;
int i,j;
cin>>i>>j;
public:
Area A1(i,j);
Area(int i, int j)
int temp;
{
length=i; breadth=j;
temp=A1.AreaCalculation();
}
A1.DisplayArea(temp);
int AreaCalculation()
return 0;
{ return (length*breadth); }
}
void DisplayArea(int temp)
{
cout<<"Area: "<<temp;
}
}; 93
Copy constructor
class Point
{
private:
int x, y;

public:
Point(int x1, int y1)
{ x = x1; y = y1; }

// Copy constructor
Point(const Point &p2)
{
x = p2.x; y = p2.y;
}

int getX() { return x; }


int getY() { return y; }
};

94
Copy constructor
class Point
{ int main()
private: {
int x, y; Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
public:
Point(int x1, int y1) // Let us access values assigned by constructors
{ x = x1; y = y1; } cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
// Copy constructor
int getX() return 0;
{ }
return x;
}
int getY()
{
return y;
}
};

95
Dynamic Constructor
class Account
{
private:

int account_no;
int balance;

public :

Account(int a,int b)
{
account_no=a;
balance=b;
}

void display()
{
cout<< "\nAccount number is : "<< account_no;
cout<< "\nBalance is : " << balance;
}
};

96
Dynamic Constructor
class Account void main()
{ {
private: clrscr();
int an,bal;
int account_no;
int balance; cout<< "Enter account no : ";
cin >> an;
public :
cout<< "Enter balance : ";
Account(int a,int b) cin >> bal;
{
account_no=a; Account *acc=new Account(an, bal); //dynamic
balance=b; constructor
}
acc->display();
void display()
{ getch();
cout<< "\nAccount number is : "<< account_no; }
cout<< "\nBalance is : " << balance;
}
};

97

You might also like