Definition and Declaration of A Class
Definition and Declaration of A Class
double length;
// Length of a box
double breadth;
// Breadth of a box
double height;
// Height of a box
DECLARATION OF OBJECTS AS
INSTANCES OF A CLASS
The objects of a class are declared after the class definition. One
must remember
that a class definition does not define any objects of its type, but it
defines the properties of a class. For utilizing the defined class, we
need variables of the class type. For example,
Largest ob1,ob2; //object declaration
will create two objects ob1 and ob2 of largest class type. As
mentioned earlier, in C++ the variables of a class are known as
objects.
These are declared like a simple variable i.e., like fundamental
data types.
Class student
{
private:
char reg_no[10];
char name[30];
int age;
char address[25];
public :
void init_data()
{
- - - - - //body of function
---- }
void display_data()
}
};
student ob; //class variable (object) created
---- ---- Ob.init_data(); //Access the member function
ob.display_data(); //Access the member function
- --- -----
#include <iostream>
class Market // Market is name of class.
{
//The { marks beginning of class declaration.
public: // Access label public
void Display() // Definition of a public function Display ()
{
cout<< Welcome To This Market << endl;
cout << Here is the price List <<endl;}
}; //Semicolon after } marks end the of class declaration.
void main() // start of main program.
{
Market L1; // declaration of object L1 of class Market
L1.Display(); // Function linked with object L1
} // dot(.) is selection operator
#include <iostream>
using namespace std;
class Market
{ // start of class declaration
public:
void Display(); // function prototype
}; // end of class
/*In the following function definition, void is typeof function, Market
is class name, :: is the scope resolution and Display is name of
function, there
are no parameters*/
void Market::Display () //function definition outside the class
{
cout<< Welcome To This Market << endl;
cout << Here is the price List <<endl;
}
void main() //main program
{Market L1; // L1 is declared an object of class market
L1.Display(); // L1 calls function display
}
#include <iostream>
using namespace std;
class Market
{
private :
int x,y,z;
public:
void Display1()
{
cout<< Welcome! Here is the price List << endl;}
void Setprice (int a, int b, int c) // function defined in class
{x = a, y = b, z = c;}
void Display2 ()
{
cout<< Price of item1 = << x<<endl;
cout<< Price of item2 = << y<<endl;
cout<< Price of item3 = << z<<endl;
}
}; // End of class
void main()
{
Market L1;
L1.Setprice (4,6,2); // Object L1 is initialized by function
// setprice()
L1. Display1(); // Functions called by using dot operator.
L1.Display2();
}
#include <iostream>
using namespace std;
class Rect { // beginning of class
public:
int x ,y; // data declared public
int Area( )
{return x*y; }
}; // End of class
int main()
{
Rect R1; //R1 is an object of class Rect
R1.x = 8; // accessing data members directly
R1.y = 5;
cout << Area = << R1.Area()<<endl;
cout << Length = << R1.x <<endl;
cout<< Width = << R1.y << endl;
return 0 ;
}
#include <iostream>
using namespace std;
class Rect {
private :
int x ,y;
public:
void setsides ( int L, int W){ x = L, y = W;}
int Area( ){return x*y;} // definition of function Area ()
// public function to get object data
void Dimension (){cout <<Length = << x <<,\tWidth = <<y
<< endl; }
}; // End of class
int main()
{
Rect R1;
R1.setsides (8,5) ; //Calling setside () to initialize x,y
cout << Area = << R1.Area()<<endl;
R1.Dimension (); // calling function Dimension ()
return 0 ;
}
#include <iostream>
using namespace std;
class Cuboid {
private:
int surface_area( ); // function declared private
int volume( ); // function declared private
int x , y, z; // data declared private
public:
Cuboid(int L,int W,int H ):x(L),y(W),z (H){} // Constructor
}; // End of class
int Cuboid::surface_area()
{return 2*(x*y +y*z +z*x);}
int Cuboid::volume()
{return x*y*z ;}
int main()
{
Cuboid C1(5,6,4);
cout << Volume of cuboid C1 << C1. volume()<<\n;
cout<< surface area of C1 = << C1. surface_area()<<\n;
return 0 ;
}
#include <iostream>
class Cuboid {
private:
int surface_area( ); // private member function
int volume( ); // private member function
int x , y, z; // private data
public:
Cuboid( int L,int W,int H ):x(L),y(W),z (H){} // Constructor
int Surface_area () { return surface_area ();}
//A public function to just pass on the return values.
int Volume () { return volume();}
//The public function for passing on the return value
// Volume and volume are different because of different case of
//first letter. So are the Surface_area and surface_area
}; // End of class
int Cuboid::surface_area() // Definition of surface_area()
{return 2*(x*y +y*z +z*x);}
int Cuboid::volume() // Definition of volume
{return x*y*z ;}
int main()
{
Cuboid C1(5,6,4);
cout << Volume of cuboid C1 << C1. Volume()<<\n;
cout<<Surface area of C1 = << C1. Surface_area()<<\n;
return 0 ;
}
LOCAL CLASSES
#include<iostream>
void fun()
{
class Test // local to fun
{
public:
void method();
};
// Error as the method is defined outside the local class
void Test::method()
{
cout << "Local Class method()";
}
}
int main()
{
return 0;
}
Output:
Compiler Error:
CONSTANT OBJECTS
class X
{
int i;
public:
X(int x) // Constructor
{
i=x;
}
int f() const // Constant function
{
i++;
}
int g()
{ i++; }
};
int main()
{
X obj1(10); // Non const Object
const X obj2(20); // Const Object
obj1.f(); // No error
obj2.f(); // No error
cout << obj1.i << obj2.i ;
obj1.g(); // No error
obj2.g(); // Compile time error
}
Output : 10 20
Type Of Constructor
Copy Constructor
It is of the form classname (classname &) and used for
the initialization of an object form another object of
same type. For example
A copy constructor may be called in the following
situations:
1. When an object is defined and initialized with other
object of the same class.
Eg:
sample s1;
sample s2=s1;
2. When we pass an object by value.
3. In case a function returns an object.
#include<iostream>
#include<conio.h>
class Example
{
int a,b; // Variable Declaration
public:
Example(int x,int y)
{
a=x;
b=y;
cout<<"\nIm Constructor";
}
void Display() {
cout<<"\nValues :"<<a<<"\t"<<b;
}
};
int main()
{
Example Object(10,20);
//Copy Constructor
Example Object2=Object;
// Variable Declaration
Object.Display();
Object2.Display();
// Wait For Output Screen
getch();
return 0;
}
#include <iostream.h>
class Exforsys()
{
private: int a;
public: Exforsys()
{}
Exforsys(int w)
{ a=w; }
Exforsys(Exforsys& e)
{
a=e.a;
cout<< Example of Copy Constructor;
}
void result()
{
cout<< a;
}
};
void main()
{ Exforsys e1(50);
Exforsys e3(e1);
cout<< \ne3=;
e3.result();
}
Default Constructor:
Parameterized Constructor
For example:
class Exforsys
{
private:
public:
Exforsys()
{}
~ Exforsys()
{}}
Constructor and Destructor Example
For example
Class student
{
Static int count; //declaration within class
------------------------------------------------};
The static data member is defined outside the class as :
We can also initialize the static data member at the time of its definition as:
Class student
{
Static int count;
----------------public :
--------------------------------static void showcount (void) //static member function
{
Cout<<count=<<count<<\n;
}
};
int student ::count=0;
Static Member Function Example
STRUCTURES
ACCESSING STRUCTURE
MEMBERS
INITIALIZATION
Initialization cannot be done inside the structure declaration
because that is a kind of blue-print or design.
The particular instances may be initialized as illustrated below
for the case of struct Employee. The method of accessing the
individual structure is illustrated above.
If it is desired to initialize the name of employee for instance,
we can write
E1.Name = Ram Dass;
The whole structure may be initialised as illustrated below.
Employee E1, E2, E3;
E1 = { Ram Dass, 30, Engineer, 20000};
E2 = {Dinesh, 25, Jr-Engineer, 15000};
E3 = {Priti, 20 , Office Manager, 1500};
A structure may be assigned to another structure.
# include <iostream>
using namespace std;
struct Employee // declaration of structure Employee.
{
char Name [30];
int Age;
char Designation [25];
int Pay ;}; // End of structure
int main ()
{ Employee E1 = { Ram Dass, 30, Engineer, 20000};
Employee E2 = {Dinesh, 25, Jr-Engineer, 15000};
Employee E3 = {Priti, 20 , Office Manager, 1500};
cout<< The data of employee E2 is :\n;
cout<< Pay <<E2.Pay<<endl;
cout<< Name << E2.Name<<endl;
cout << Age << E2.Age <<endl;
cout<< Designation << E2.Designation<<endl;
return 0;
}
# include <iostream>
using namespace std;
struct Employee
{ char Name [30];
int Age;
char Designation [25];
int Pay ;}; // end of declaration
int main ()
{ Employee E1 = { Ram Dass, 30, Engineer, 20000};
Employee E2 = { Dinesh, 25, Jr-Engineer, 15000};
Employee E3 = { Priti, 20 , Office Manager, 15000};
cout<< The data of employee E2 is :\n;
cout<< Pay <<E2.Pay<<endl;
cout<< Name << E2.Name<<endl;
cout << Age << E2.Age <<endl;
cout<< Designation << E2.Designation<<endl;
E1 = E3; // assignment of structure
cout<< Pay <<E1.Pay<<endl;
cout<< Name << E1.Name<<endl;
cout << Age << E1.Age <<endl;
cout<< Designation << E1.Designation<<endl;
return 0;
}
C++ STRUCTURES
#include<iostream>
#include <string>
using namespace std;
struct Employee
{
private :
int Pay, Employee_code ;
public:
Employee ( int E,int P ) { Employee_code =E , Pay = P ;}
// constructor function
int getpay ( ) {return Pay ;} // functions
int getcode (){return Employee_code ;}
};
int main ()
{
Employee E1 (22, 10000);
Employee E2 (32, 15000);
cout<< Employee_code of E1 = << E1.getcode ()<<endl;
cout<< Pay of E1 = <<E1.getpay()<<endl;
cout<< Employee_code of E2 = << E2.getcode ()<<endl;
cout<< Pay of E2 = <<E2.getpay()<<endl;
return 0;
}
POINTER TO A CLASS
pointer to a class
The above program also shows that for calling a function of
class in the main ()through a pointer ptr, either of the
following two codes may be used.
ptr -> Display3();
or
(*ptr).Display3();
Both the above statements are equivalent. In the second
statement *ptr has to be enclosed in brackets because the
dot (.) operator has higher precedent than the dereference
operator (*).
POINTERS TO OBJECTS OF A
CLASS
What is a Friend
Function?
Class A
{
int a;int b;
public:
void set_value(int m,int n)
{
a=m;
b=n; }
void put_value()
{ cout<<m<<n; }
};
int square( A &x)
{int t=x.a *x.a + x.b*x.b; /* error.
accessing private data members of object*/
return t;
}
FRIEND CLASSES
Class B
{
friend class A; //declaration that class A is friend of B
private:
statements
friend class C; //declaration that class C is friend of B
public:
Other_statements;
}
Example
Friend-Rules