Objects and Classes
Objects and Classes
IMPORTANT QUETIONS FOR EXAM (GTU,CVM,SP)
CHAPTER ONE COMPLETE
HERE IS YOUR CVMU SYLLABUS -LINK
BLOG BY JAY SHAH(GMAIL)
Explain basic concept of class.
A class is a template that specifies the attributes and behavior of things or objects.
A class is a blueprint or prototype from which objects are created.
A class is the implementation of an abstract data type (ADT).
It defines attributes and methods.
Object declaration:
In following example class employee is created and ‘a’ is object of this class.
class item
{
// data members and member functions
}a;
In above syntax class name is item, and a is object of that class
Object declaration can be also done in main() function as follows:
int main()
{
item a;
}
Accessing class member:
Private members of the class can only accessed by the members with in that class.
Public members of the class can be accessed outside the class also.
For accessing class member outside class can be done by using dot operator and object of that class using
following syntax,
object-name.function-name(actual-arguments);
Member functions of the class can be declared outside class definition also as follows,
Syntax:
class item
{
public:
void getdata();
};
void item::getdata();
Example:
#include<iostream>
using namespace std;
class employee // class
{
char name[10]; // data member
int id; // data member
public:
void getdata(); // prototype declaration
void putdata(); // prototype declaration
};
void employee::getdata() // member function
{
cout<<”Enter name and id of employee: ”;
cin>>name>>id;
}
void employee::putdata() // member function
{
cout<<”Display name and id of employee: ”;
cout<<name<<id;
}
int main()
{
employee x;
x.getdata();
x.putdata();
}
Explain Public and Private Access modifier (specifier) for C++ classes.
Public:
Public members of the class are accessible by any program from anywhere.
There are no restrictions for accessing public members of a class.
Class members that allow manipulating or accessing the class data are made public.
Private:
Private members of the class can be accessed within the class and from member functions of the class.
They cannot be accessed outside the class or from other programs, not even from inherited class.
Encapsulation is possible due to the private access modifier.
If one tries to access the private members outside the class then it results in a compile time error.
If any other access modifier is not specified then member default acts as Private member.
Example:
#include<iostream>
using namespace std;
class ABC
{
public:
int a;
private:
int b;
};
int main()
{
ABC x;
x.a=5;
cout<<"Value of public variable = "<<x.a;
}
Output:
5
In above program b is private member of class ABC.
So we cannot use it with object of ABC outside the class, Means we cannot use x.b in main as x.a shown
above.
Explain Static data members and static member functions with example.
Static data members:
Data members of the class which are shared by all objects are known as static data members.
Only one copy of a static variable is maintained by the class and it is common for all objects.
Static members are declared inside the class and defined outside the class.
It is initialized to zero when the first object of its class is created. No other initialization is permitted.
It is visible only within the class but its lifetime is the entire program.
Static members are generally used to maintain values common to the entire class.
Static member functions:
Static member functions are associated with a class, not with any object.
They can be invoked using class name, not object.
They can access only static members of the class.
They cannot be virtual.
They cannot be declared as constant or volatile.
A static member function can be called, even when a class is not instantiated.
There cannot be static and non-static version of the same function.
A static member function does not have this pointer.
Example:
#include<iostream>
using namespace std;
class item
{
int number;
static int count; // static variable declaration
public:
void getdata(int a)
{
number = a;
count++;
}
static void getcount() // the only difference from above program
{
cout<<”value of count: “<<count;
}
};
int item :: count; // static variable definition
int main()
{
item a,b,c;
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
getch();
return 0;
}
Output:
value of count:3
value of count:3
value of count:3
What is constructor? List out characteristics of constructors.
A constructor is a “special” member function which initializes the objects of class.
Properties of constructor:
Constructor is invoked automatically whenever an object of class is created.
Constructor name must be same as class name.
Constructors should be declared in the public section because private constructor cannot be invoked
outside the class so they are useless.
Constructors do not have return types and they cannot return values, not even void.
Constructors cannot be inherited, even though a derived class can call the base class constructor.
Constructors cannot be virtual.
An object with a constructor cannot be used as a member of a union.
They make implicit calls to the operators new and delete when memory allocation is required.
Explain types of constructor with example.
There are mainly three types of constructors as follows:
1. Default constructor:
Default constructor is the one which invokes by default when object of the class is created.
It is generally used to initialize the value of the data members.
It is also called no argument constructor.
Example:
class integar
{
int m,n;
public:
integer() // Default constructor
{
m=n=0;
}
};
2. Parameterized constructor
Constructors that can take arguments are called parameterized constructors.
Sometimes it is necessary to initialize the various data elements of different objects with different
values when they are created.
We can achieve this objective by passing arguments to the constructor function when the objects are
created.
Example:
class integer
{
int m,n;
public:
integer(int x,int y) // Parameterized constructor
{
m =x;
n=y;
}
};
3. Copy constructor
A copy constructor is used to declare and initialize an object from another object.
For example, integer(integer &i); OR integer I2(I1);
Constructor which accepts a reference to its own class as a parameter is called copy constructor.
Example:
class integer
{
int m, n;
public:
integer(rectangle &x) // Copy constructor
{
m = x.m;
n = x.n;
}
};
Example:
#include<iostream>
using namespace std;
class rectangle
{
int length, width;
public:
rectangle() // Default constructor
{
length=0;
width=0;
}
rectangle(int _length, int _width) // Parameterized constructor
{
length = _length;
width = _width;
}
rectangle(rectangle &_r) // Copy constructor
{
length = _r.length;
width = _r.width;
}
....................................
// other functions for reading, writing and processing can be written
here
....................................
};
int main()
{
rectangle r1; // Invokes default constructor
rectangle r2(10,10); // Invokes parameterized constructor
rectangle r3(r2); // Invokes copy constructor
}
Explain destructor with example.
Destructor is used to destroy the objects that have been created by a constructor.
Destructor is a member function whose name must be same as class name but is preceded by a tilde (~).
Destructor never takes any argument nor it returns any value nor it has return type.
Destructor is invoked automatically by the complier upon exit from the program.
Destructor should be declared in the public section
Example:
#include<iostream.h>
using namespace std;
class rectangle
{
int length, width;
public:
rectangle() //Constructor
{
length=0;
width=0;
}
~rectangle() //Destructor
{
}
// other functions for reading, writing and processing can be written
here
};
int main()
{
rectanble x; // default constructor is called for this object
}
Explain operator overloading with example.
Operator overloading is compile time polymorphism.
The operator overloading provides mechanism to perform operations on user defined data type.
We can give special meaning to any operators in which program it is implemented.
Rules for operator overloading
1. Only existing operator can be overloaded.
2. The overloaded operator must have at least one operand that is user defined type.
3. We cannot change the basic meaning and syntax of an operator.
4. We cannot use friend function to overload certain operators.
5. Unary operators, overloaded by means of a member function, take no explicit arguments and return
no explicit value,
But, those overloaded by means of a friend function, take one reference argument.
6. Binary operators overloaded through a member function take one explicit argument and those which
are overloaded through a friend function take two explicit arguments.
7. When using binary operators overloaded through a member function, the left hand operand must be
an object of the relevant class.
8. We cannot overload following operators.
Operator Name
. and .* Class member access operator
:: Scope Resolution Operator
sizeof() Size Operator
?: Conditional Operator
Example for Unary Operator Overloading
#include <iostream>
using namespace std;
class sample
{
int a,b;
public:
void getdata()
{
a=10;
b=20;
}
void operator -() //Unary Member Function
{
a = a - 5;
b = b - 5;
}
void disp()
{
cout<<"\nThe value of a="<<a;
cout<<"\nThe value of b="<<b;
}
};
int main()
{
sample S;
S.getdata();
-S; //Call Unary Member Function
S.disp();
getch();
return 0;
}
Output:
The value of a=5
The value of b=15
Example for Unary Operator Overloading using Friend function.
#include <iostream>
using namespace std;
class sample
{
int a,b;
public:
void getdata()
{
a=10;
b=20;
}
friend sample operator +(sample A) //Unary Friend Function
{
A.a = A.a + 5;
A.b = A.b + 5;
return A;
}
void disp()
{
cout<<"\nThe value of a="<<a;
cout<<"\nThe value of b="<<b;
}
};
int main()
{
sample S;
S.getdata();
S=+S; //Call Unary Friend Function
S.disp();
return 0;
}
Output:
The value of a=15
The value of b=25
Example for Binary Operator Overloading
#include <iostream>
using namespace std;
class complex
{
float real, imag;
public:
complex(float _real, float _imag) // constructor
{
real = _real;
imag = _imag;
}
void disp()
{
cout<<"The value of real="<<real;
cout<<"The value of imag="<<imag;
}
void operator +(complex c) //Binary Member function
{
real = real + c.real;
imag = imag + c.imag;
}
};
int main()
{
complex x(4,4);
complex y(6,6);
x + y; // Call Binary Member Function
x.disp();
getch();
return 0;
}
Output:
The value of real=10
The value of imag=10
Example for Unary Operator Overloading using Friend function.
#include <iostream>
using namespace std;
class complex
{
float real, imag;
public:
complex(float _real, float _imag) // constructor
{
real = _real;
imag = _imag;
}
void disp()
{
cout<<"The value of real="<<real;
cout<<"The value of imag="<<imag;
}
friend complex operator +(complex c, complex d) //Binary Friend function
{
d.real = d.real + c.real;
d.imag = d.imag + c.imag;
return d;
}
};
int main()
{
complex x(4,4);
complex y(6,6);
complex z = x + y; // Call Binary Friend Function
z.disp();
}
Output:
The value of real=10
The value of imag=10
List out various type conversion techniques? Explain basic to class type conversion with example.
C++ provides mechanism to perform automatic type conversion if all variable are of basic type.
For user defined data type programmers have to convert it by using constructor or by using casting
operator.
Three type of situation arise in user defined data type conversion.
1. Basic type to Class type
2. Class type to Basic type
3. Class type to Class type
Now we will see each situation with example
1. Basic type to Class type
The basic type to class type conversion is done by using constructor.
Example:
#include <iostream>
using namespace std;
class sample
{
float a;
public:
sample(){}
sample(int x) //Constructor to convert Basic to Class type
{
a=x;
}
void disp()
{
cout<<"The value of a="<<a;
}
};
int main()
{
int a=10;
sample S;
S=a; //Basic to class type conversion
S.disp();
return 0;
}
Output:
The value of a=10
Explain type conversion for class to basic type with example.
The Class type to Basic type conversion is done by using Casing Operator.
The casting operator function should satisfy the following conditions.
1. It must be a class member.
2. It must not specify a return type.
3. It must not have any arguments.
Example:
#include <iostream>
using namespace std;
class sample
{
float a;
public:
sample()
{
a=10.23;
}
operator int();
};
sample:: operator int() //Casting operator function
{
int x;
x=a;
return x;
}
int main()
{
sample S;
int y= S; // Class to Basic conversion
cout<<"The value of y="<<y;
getch();
return 0;
}
Output:
The value of y=10
Comments
Post a Comment