Thursday, 28 April 2016

practice questions for class & Object

1.     Define a class Travel in C++ with the description given below:
Private members:
plancode of type long place of type characters array
 number_of_travellers of type integer
 number_of_buses of type integer
Public members:
 A constructor to assign initial values of plancode as 1001, place as “Kolkata”, number_of_travellers as 5 and number_of_buses as 1
A function newplan( ) which allows user to enter plancode , place and number_of_travellers and also assign the number_of_buses as per the following conditions:
number_of_travellers        number_of_buses
less than 20                                         2
equal to and more than 20
 and less than 40                                3
equal to and more than 40               4
 A function show( ) to display the contents of all the data members on the screen
2. Answer the questions (i) to (iv) based on the following code :
class Goods {
int id; protected :
char name[20]; long qty;
void Incr(int n);
public :
Goods(); ~Goods();
void get();
};
class Food_products : public Goods {
char exp_dt[10];
protected : int id; int qty;
public :
void getd();
void showd();
};
class Cosmetics : private Goods {
 int qty; char exp_date[10];
protected : int id;
 public :                                                 
~Cosmetics();
Cosmetics();
 void show();
};




(i)                How many bytes will be required by an object of class Food_products.
(ii)               (ii) Name the member functions accessible through the object of class Food_products.
(iii)             From the following, Identify the member function(s) that cannot be called directly from the object of class Cosmetics show(), getd(), get()
(iv)             (iv) If the class cosmetics inherits the properties of food_products class also, then name the type of inheritance
33 .     Describe friend function with example.
44 .     what is static data member and static function
55 .     Explain nested class with suitable  program.
66 .     Write four  features of constructor and destructor
77 .       Write the following law of Boolean algebra and prove with truth table
a)     Absorption law  b) distributive law    c) complementary law   d) involution law
e)  Idempotent  law  f) associative law
8) Write D Morgan’s theorem and prove it algebraically
9) Define the following terms
a) keyword   b)literals   c) identifier d) default parameter e) function prototype   f) type casting   g) explicit data type conversion   h) random()   i) randomize()  j) header files   k


Sunday, 17 April 2016

CONSTRUCTOR AND DESTRUCTOR


CONSTRUCTOR AND DESTRUCTOR
We initialize the built in data type during declaration like int a=5
We also initialize array during declaration int a[ ]={1,2,3,4};
Char name[ ]=”Sachin”;
We also initialize structure like given below
#include<iostream.h>
#include<conio.h>
struct player
{
int id,age;
char name[20];
};
void main()
{
player p={7,32,"Dhoni"};
cout<<p.id<<"-"<<p.name<<"-"<<p.age<<endl;
getch();
}
Here is the question how we can initialize the class?

Solution is constructor
Before learning how to define constructor we should know more about Feature of constructer
Features of constructor
1.       It has no return data type
2.       It is also like a member function
3.       Name of class and constructor is always same
4.       It is implicitly called
5.       It can be overloaded
6.       It can’t be inherited but derived class can call base class constructor
7.       It follows the access rule
8.       When object is declared constructor function will be invoke according to matching parameter

Defining constructor
class student
{
int age;
public:
student()      // constructor
{
}
void get();
void show();
};
Here we have defined one constructor we can see that it is defined like function, name of constructor and class is same, no return type even void
When we declare object student s constructor will be invoked. Here we can see that no value is passed in constructor so no value will be passed in object
We can also define more than one constructor in a class. As we know that it can be overloaded. When more than one constructor is defined constructor will be called according to matching of arguments.
Ex.
class student
{
int age;
public:
student()      // constructor  1
{
}
Student(int a) // constructor 2
{
age=a;
}
void get();
void show();
};
If we declare object student s constructor 1 will be invoked as its argument is matching, similarly when we declare object like
student s(5);
Constructor 2 will be invoked

Destructor:- as we have discussed constructor is used to initialize the object. If we declare so many objects in a program then each object will occupy memory. If some constructor is not in use memory occupied by it must be released.  C++ compiler has one interesting features which release the memory occupied by an object when its life time is over

Destructor is a member function which destroy the object/release the memory occupied by object when its life time is over
It can be define like
~student()
{
}
We use ~(Tild) sign to define destructor

Features of destructor is almost same like constructor. Main difference between constructor and destructor is that constructor is used to initialize the object while destructor is used to destroy the object. only one destructor can be defined for class but destructor can't be overloaded.
Features of destructor
1.       It has no return data type
2.       It is also like a member function
3.       Name of class and destructor is always same
4.       It is implicitly called when life time of object is over.
5.       It can’t be inherited but derived class can call base class constructor
6.       It follows the access rule
7.       When object is declared destructor function will be invoke according to matching parameter

      Types of constructor

1.       Default constructor
2.       Parametrized constructor
3.       Copy constructor

1.       Default constructor
Student()
{
}
Above define constructor is called default constructor  which does not have any arguments
When we declare object
student s;
default constructor will be invoked
student ()
{
Age =15;
}
This is also example of default constructor it also not having argument it will also called when student s ; object will be declared and it will initialize the age of student with 10.
Note:-  student() { } and student() { age =15} can’t be define in same class as it is not
satisfying the overloading rules.

2.       Parametrized constructor:
student(int a)
{
age=a;
}
For calling this constructor we have to declare object with matchin argument like
Student s(5);
3.    Copy constructor :- copy constructor is used to copy one object into another object.
If we talk about built in data type we assign value of one variable into other like
int a =5;
this type of initialization is called explicit assignment
there is another method of assignment int a(5);
we also assign object of structure like student s1,s2;
s1=s2
is there any method to assign one class object into another object of same class. It is possible with the help of copy constructor
copy constructor always takes one argument of class object in call by reference mode
Example student(student &s)
example of copy constructor
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
int roll,marks;
char name[10];
public:
student()
{
cout<<"\n default constructor"  ;
}
student(int r, int m, char n[])
{
cout<<"\n parametrized constructor";
roll=r;
strcpy(name ,n);
marks =m;
}
student(student &s)
{
cout<<"\n copy constructor\n";
roll= s.roll;
strcpy(name,s.name);
marks=s.marks;
}
~student(){
cout<<"\n destructor\n";
}
void get()
{
cout<<"\n enter roll"  ;
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter marks\n";
cin>>marks;
}
void show()
{
cout<<roll<<"\t"<<name<<"\t"<<marks<<endl     ;
}
};
void main()
{
{
student s(5,10,"Rahul");  // parametrized constructor is called
student m ;              // default constructor is called
m.get();
student n(m); // copy constructor is called
m.show();
s.show();
n.show();
getch();
}
getch();
}

output of the program


Practice Questions





Friday, 8 April 2016

CLASS AND OBJECT
CLASS:-           CLASS IS ABSTRACT USER DEFINED DATA TYPE
                                   CLASS IS WAY OF BINDING DATA AND METHOD TOGETHER
                                   CLASS IS GROUP OF SIMILAR KIND OF OBJECTS
Defining a class in c++
Student
Roll
Name
Age
Marks

Void get() // to accept student details
Void show()// to display student details

If we have to define class in C++ for the above class represented in diagram we can define like this
#include<iostream.h>
#include<conio.h>
class student
{
int roll,age,marks;       // by default data are private, private data are not accessible out side class
char name[20];
public:                     // functions are public means it will be accessible outside the class using object of class
void get()
{
cout<<"\n enter roll";
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter age";
cin>>age;
cout<<"\n enter roll";
cin>>marks;
}
void show()
{
cout<<"Name:"<<name<<endl;
cout<<"Noll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Marks:"<<marks<<endl;
}
};
void main()
{
student s;
s.get();
s.show();
getch();
}
Here function is defined inside the class. We can also define function outside the class as give below
#include<iostream.h>
#include<conio.h>
class student
{
int roll,age,marks;                   // by default data are private, private data are not accessible outside class
char name[20];
public:                           // functions are public means it will be accessible outside the class using object of class
void get(); // function prototype
void show();// function prototype
};
void student::get() // function is define using scope resolution operator , student::get() mean get of student class
{
cout<<"\n enter roll";
cin>>roll;
cout<<"\n enter name";
cin>>name;
cout<<"\n enter age";
cin>>age;
cout<<"\n enter roll";
cin>>marks;
}
void student::show()               // function is defined  using scope resolution operator , student::show() mean show                                                    of student class
{
cout<<"Name:"<<name<<endl;
cout<<"Noll:"<<roll<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Marks:"<<marks<<endl;
}
void main()
{
student s;
s.get();
s.show();
getch();
Access modifiers :- (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members.
1.    Public :- accessible every where
2.    Protected :- accessible inside the class and in derived class
3.    Private:- accessible only by member function of class.

Using class in main Program
Class can be used in main program by declaring object
Since private members are not accessible outside object can access only public member
As shown in given example
void main()
{
student s;          // s is object of class student
s.get();                    // accessing get function(using .dot operator) of student class as it is public
s.show();                // accessing show function(using .dot operator) of student class as it is public
getch();
}
Array of object
Student s can store details of one student. If we have to store details of more than one student we can create array of student object like
Student s[5];
We can call the function student[i].get()
Example
void main()
{
student s[5];          //five  object of class student
for(i=0;i<5;i++)
{
S[i].get();              
}
for(i=0;i<5;i++)
{
S[i].show();          
}
getch();

}

}