UNIT-II
Classes & Objects
- Write a C++ program to count the number of objects of a certain class.[DEC/JAN13]
#include <iostream
using namespace std;
class counter{
static int count;
Counter() { count++; }
~Counter() { count--; }
};
int Counter::count; void f();
int main(void) {
Counter o1;
cout < "Objects in existence: ";
cout < Counter::count < "\n";
Counter o2;
cout < "Objects in existence: ";
cout < Counter::count < "\n"
f();
cout < "Objects in existence: "; cout < Counter::count < "\n";
return 0;
}
void f()
{
Counter temp; cout < "Objects in existence:
";
cout < Counter::count < "\n"; // temp is destroyed when f() returns
}
}
2. What is a class? How is it created? Write an example class[DEC/JAN 14]
A class is a mechanism for creating user-defined data types. It is similar to the C language structure data type. In C, a structure is composed of a set of data members. In C++, a class type is like a C structure, except that a class is composed of a set of data members and a set of operations that can be performed on the class.
In C++, a class type can be declared with the keywords union, struct, or class. A union object can hold any one of a set of named members. Structure and class objects hold a complete set of members. Each class type represents a unique set of class members including data members, member functions, and other type names. The default access for members depends on the class key:
•The members of a class declared with the keyword class are private by default. A class is inherited privately by default.
•The members of a class declared with the keyword struct are public by default. A structure is inherited publicly by default.
•The members of a union (declared with the keyword union) are public by default. A union cannot be used as a base class in derivation.
Once you create a class type, you can declare one or more objects of that class type. For example:
class X
{
/* define class members here */
};
int main()
{
X xobject1; // create an object of class type X
X xobject2; // create another object of class type X
}
You may have polymorphic classes in C++. Polymorphism is the ability to use a function name that appears in different classes (related by inheritance), without knowing exactly the class the function belongs to at compile time.
C++ allows you to redefine standard operators and functions through the concept of overloading. Operator overloading facilitates data abstraction by allowing you to use classes as easily as built-in types.
3. What are constructors? How are they different from member functions?[DEC/JAN 14.DEC/JAN 15.]
A constructor is similar to a function, but with the following differences.
•No return type.
•No return statement.
#define POINT_H class Point { public:
Point(); // parameterless default constructor
Point(int new_x, int new_y); // constructor with parameters int getX(); int getY();
private: int x; int y;
};
#endif
Here is part of the implementation file.
//=== point/point.cpp ======
. . .
Point::Point()
{
// default constructor x = 0; y = 0;
}
Point::Point(int new_x, int new_y)
{
// constructor x = new_x;
y = new_y;
}
. . .
And here is part of a file that uses the Point class.
//=== point/main.cpp ======
. . .
Point p; // calls our default constructor
Point q(10,20); // calls constructor with parameters
Point* r = new Point(); // calls default constructor
Point s = p; // our default constructor not calle
4. What are static data members? Explain with an example what the use of static data members is.[DEC/JAN 15,JUN/JULY 15. ]
When you precede a member variable's declaration with static, you are telling the compiler that only one copy of that variable will exist and that all objects of the class will share that variable. Unlike regular data members, individual copies of a static member variable are not made for each object. No matter how many objects of a class are created, only one copy of a static data member exists. Thus, all objects of that class use that same variable. All static variables are initialized to zero before the first object is created.
When you declare a static data member within a class, you are not defining it. (That is, you are not allocating storage for it.) Instead, you must provide a global definition for it elsewhere, outside the class. This is done by redeclaring the static variable using the scope resolution operator to identify the class to which it belongs. This causes storage for the variable to be allocated. (Remember, a class declaration is simply a logical construct that does not have physical reality.) To understand the usage and effect of a static data member, consider this program:
#include <iostream> using namespace std; class shared { static int a; int b;
public: void set(int i, int j)
{a=i; b=j;}
void show();
} ;
int shared::a; // define a void shared::show()
{
cout < "This is static a: " < a; cout < "\nThis is non-static b: " < b; cout < "\n";
}
int main()
{
shared x, y;
x.set(1, 1); // set a to 1
x.show();
y.set(2, 2); // change a to 2
y.show();
x.show(); /* Here, a has been changed for both x and y because a is shared by both objects. */
return 0;
}
This program displays the following output when run.
This is static a: 1
This is non-static b: 1
This is static a: 2
This is non-static b: 2
This is static a: 2
This is non-static b: 1
5. Demonstrate with C++ program for
i) Passing objects to functions ii)Returning objects [DEC/JAN 13.]
.i) Objects may be passed to functions in just the same way that any other type of variable can. Objects are passed to functions through the use of the standard call-by-value mechanism. Although the passing of objects is straightforward, some rather unexpected events occur that relate to constructors and destructors. To understand why, consider this short program.
// Passing an object to a function.
iostream> namespace std;
class myclass {
public:
myclass(int n);
~myclass();
voidset_i(int n)
{ i=n; }
intget_i() { return i; }
};
myclass::myclass(int n)
{
i = n;
cout < "Constructing " < i < "\n";
}
myclass::~myclass() {
cout < "Destroying " < i < "\n";
}
void f(myclassob);
main()
{ myclass
f(o);
cout < couto.get_i() < "\n";
return 0; }
void f(myclassob)
{
ob.set_i(2);
cout < "This is local i: " < ob.get_i(); cout < "\n";
}
This program produces this output:
Constructing 1
This is local i: 2
Destroying 2
This is i in main: 1
Destroying 1
ii)Returning objects
#include <iostream> using namespace std;
classmyclass {
inti;
public: void set_i(int n)
{ i=n; }
intget_i() { return i; }
};
myclass f(); // return object of type myclass int main()
{ myclass o; o = f();
couto.get_i() < "\n";
return 0; }
myclass f() {
myclass x; x.set_i(1); return x;
}