Original PDF
Original PDF
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.
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 (.)
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
double getVolume(void);
63
Example 2
// Member functions definitions
// 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 );
};
int main( )
{
Line line;
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.
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;
};
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;
}; }
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
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:
81
EXAMPLE 1
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor
private:
double length;
};
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);
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;
};
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;
};
public:
Area()
{
length=5; breadth=5;
}
void GetLength()
{
cout<<"Enter length and breadth respectively: ";
cin>>length>>breadth;
}
int AreaCalculation()
{
return (length*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);
}
class Area
{
private:
int length;
int breadth;
public:
Area(int i, int j)
{
length=i; breadth=j;
}
int AreaCalculation()
{ return (length*breadth); }
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;
}
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