Classes and Objects in C++
Classes
A class binds data describing an entity and its associated functions together.
Ex.
An account having characteristics account no, type and
balance .Its associated operations are deposit and withdrawal.
Its class defined as follows
Declaration of class evolves four associated functions-
1.
Data
members are the data type properties that describe the
characteristics of class; it may be zero or many.
Note-by default all the members of the
class are private.
- Defining a class-
class salesman{int salesman_no;
char s_name[25];
int pro_no;
float tar, sales_made,comm;
void calc_com(void);
public:
void readData();
float addman_sale(float sales);
void printData();
}
· The class method’s(member function) Definition
1. Outside the class definition
2. Inside the class definition
1. outside the class definition
The definition of the member function
outside the class is given by its qualified name (with class name)
This is done by :: (symbol),called scope resolution operator, which shows
that the function is restricted to the class?
Syntax
return-type class-name::
function-name(parameter list)
{
Function body;
}
Ex. void salesma::readData()
{
cout<<"Enter Salesman no:";
cin>>salesma_no;
cout<<"Enter salesman name";
gets(s_name);
cout<<"Enter Product_no";
cin>>pro_no;
cout<<"Enter target and sales made";
cin>>tar>>sales_made;
}
2. Inside the class
The function definition inside the class
defined in general way
· Referencing class member/declaration of object
as instance of class
For eg. sample s1,s2;
The members of the class are referenced
using object of class.
Ex.
The private data can be accessed only
through member function. But public data can be accessed through the object of
class by non-member-function. The general form for calling member function is:
object-name.
function-name (actual-arguments);
object-name.
public data-member ;
Ex.
O1.add(2,3);
O1.z=7; //valid
O1.x=5; //invalid
O1.y=3; //invalid
·
Program to illustrate the working of class and
object and private and public modifier
· Global object
Ex.
· Local object
Ex.
#include<iostream.h>
class x //Global class
{
public:
int a;
void fnc(void);
};
void main()
{
class y
{
public:
int i;
void fc(void);
};
x ob1; //local object of global class
y ob2; //local object of local class
ob1.a=5;
ob1.fnc();
ob2.i=15;
ob2.fun();
}
void funct(void)
{
x ob3;
y ob4; //invalid
ob3.a=25; //valid
ob3.fnc; //valid
ob1.a=10; //invalid
ob2.fc(); //invalid
}
· Program to illustrate the working of class and
object.
#include<iostream.h>
class temp
{
int s1;
float s2;
public:
void int_data(int d)
{
s1=d;
cout<<"Number:"<<s1;
}
float float_data()
{
cout<<"\n Enter data:";
cin>>s2;
return s2;
}
};
void main()
{
temp t1,t2;
t1.int_data(12);
cout<<"You entered:"<<t2.float_data();
}
output:-
Number:12
Enter data:12.43
You entered:12.43
#include<iostream.h>
void test(int);
void test(float);
void test(int,float);
void main()
{
int a=5;
float b=5.5;
test(a);
test(b);
test(a,b);
}
void test(int var)
{
cout<<"Integer number:"<<var<<endl;
}
void test(float var)
{
cout<<"Float number:"<<var<<endl;
}
void test(int var1,float var2)
{
cout<<"Integer Number:"<<var1<<"And float number:"<<var2;
}
· Object as function arguments
2. By reference
1. By value
When
the object is passed by value, the function creates its own copy of the object
and works with its own copy. Therefore, any changes made to the object inside
the function don’t affect the original object.
Ex.
#include<iostream.h>
class rational
{
private:
int num,dnum;
public:
void get()
{
cout<<"Enter numerator";
cin>>num;
cout<<"Enter denomenator";
cin>>dnum;
}
void print()
{
cout<<'num'<<"\"<<dnum<<endl;
}
};
void rational multi(rational r1,rational r2)
{
num=r1.num*r2.num;
dnum=r1.dnum*r2.dnum;
}
void main()
{
rational r1,r2,r3;
r1.get();
r2.get();
r3.multi(r1,r2);
r3.print();
}
This post is about Classes and Object in C++ .In the next post we will talk about Constructors and Destructors.
0 Comments