4 Class Object
4 Class Object
class FirstClass {
private:
int dataone;
int datatwo;
public:
void setdata(int o, int t) { dataone = o; datatwo = t }
void showdata()
{ cout << “\nData are “ << dataone << datatwo; }
}; Definition ends with
semicolon just like
structure
private:
int dataone;
int datatwo;
public:
void setdata(int o, int t) { dataone = o; datatwo = t }
void showdata()
{ cout << “\nData are “ << dataone << datatwo; }
};
10
11
12
16
18
// box 1 specification
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
// box 2 specification
Box2.setLength(12.0);
Box2.setBreadth(13.0);
Box2.setHeight(10.0);
// volume of box 1
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
return 0; 19
}
Dr. Alekha Kumar Mishra
Runtime objects
box1
length
breadth
height
Creating objects of Box class
Box box1, box2, box3;
box3
length
breadth
height
20
23
24
27
31
34
calling:
Box box1; // ctor called with default args
Box box2();; // ctor called with default args
Box box3(5.0); // ???
Box box4(10.0,10.0,10.0);
35
class Distance{
int main(){
Distance *ob1= new Distance();
ob1->getdist();
ob1->showdist();
delete ob1;
Distance *ob2= new Distance();
ob2->getdist();
ob2->showdist();
delete ob2;
return 0; 36
}
Dr. Alekha Kumar Mishra
Array of objects initialization
#include <iostream>
using namespace std;
class Number {
int i;
public:
Number(int j) { i=j; }
int get_i() { return i; }
};
int main() {
Number ob[3] = {1, 2, 3}; //short version of initialization
Number ob[3] = { Number(1), Number(2), Number(3) };
//longer form of initialization
... 37
}
Dr. Alekha Kumar Mishra
Array of objects initialization
●
If an object's constructor requires two or more
arguments, you will have to use the longer
initialization form
●
Example :
class Coordinate2d {
int x;
int y;
public:
Coordinate2d(int j, int k) { x=j; y=k; } // constructor with 2 parameters
...
};
int main() {
Coordinate2d ob[3] = { Coordinate2d (1, 2), Coordinate2d (3, 4),
Coordinate2d (5, 6) };
... 38
}
Dr. Alekha Kumar Mishra
What is the problem with this?
#include <iostream>
using namespace std;
class Number {
int i;
public:
Number(int j) { i=j; }
int get_i() { return i; }
};
int main() {
Number ob[3];
...
}
39
41
●
Why copy constructor argument is passed as
const reference?? 48
49
50
class Distance {
private:
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0) { }
Distance(int ft, float in) : feet(ft), inches(in) { }
...
void showdist() const {
cout << feet << "\'-" << inches << '\"';
} 51
};
Dr. Alekha Kumar Mishra
Static member of a class
●
Static data member is an attribute that is a part of class, yet
is not a part of an object
●
In otherwords, there is exactly one copy of a static member
instead of one copy per object
●
A Function that needs access to members of a class, yet
does not need to be invoked for a particular object is called
static member function
●
Often used when declaring class constants (since you generally only
need one copy of a constant)
● To make a field static, add the static keyword in front of the field
– can refer to the field like any other field
– static variables are also considered to be global, you can refer to them
without an instance static fields can be initialized
52
53
main(){
cout<<“no of Employees: “<< Employee::getCount()<<endl;
Employee* e1=new Employee (“Bob”);
e1->incCount();
Employee* e2=new Employee (“John”);
e2->incCount();
cout<<“no of employees: “ <<Employee::getCount(); 54
}
Dr. Alekha Kumar Mishra
class Employee{
char* Name; A complete example
static int count;
public:
main(){
Employee(char*); cout<<“no of Employees: “<<
char* getName(); Employee::getCount()<<endl;
static int getCount();
Employee* e1= new Employee(“Bob”);
~Employee();
Employee* e2=new Employee (“John”);
};
cout<<“no of employees: “ <<
int Employee::count=0; e1->getCount();
int Employee::getCount()
cout<<“Emp1: “<<e1->getName();
{return Count;}
Employee::Employee(char* N){ cout<<“Emp2: “<<e2->getName();
Name=new char[strlen(N)+1]; delete e1;
strcpy(Name,N); cout<<“no of employees: “
++count;
<<e2->getCount();
}
Employee::~Employee(){ delete e2;
delete [] Name; cout<<“no of employees: “ <<
--count; Employee::getCount();
} }
char* Employee::getName(){
return Name; 55
}
Dr. Alekha Kumar Mishra
Friend function
●
There could be a situation where we would like two
classes to share a particular fuction
●
Example
– findarea() can be shared by rectangle and triangle class
– income_tax() function by manager and clerks class
●
C++ allows the common function to be made
friendly with both the classes.
●
The friendly function is allowed to access to the
private data of these classes
56
57
58
class X {
...
friend class Y;
...
};
61
63