Unit 2
Unit 2
Public:
Int x;
//default constructor
Myclass ()
Count++
}
};
Int main ()
Return 0;
Member function of a class can also be declared as static. They have the
following properties:
#include<iostream>
Class myclass
Private:
Int x;
Public:
Myclass() // default constructor
Count++;
Return count;
};
//initialization
Int myclass::count = 0;
Int main ()
Cout<<”initial count”<<myclass::getcount()<<endl;
Return 0;
Que2. Explain the use of constructor and destructors in c++ with the
help of an example.
Constructors
Defining a constructor
feet = f;
inches = I;
Calling a constructor:
Types of constructor:
a) default constructor
Distance ()
Feet =0;
Inches = 0.0f;
b) Parameterized constructor
Feet = f;
Inches = I;
c) Copy constructor
A copy constructor is used in the situation when we want to declare
an initialize an object from another object. A constructor takes the
reference of another object of the same class as a parameter.
Int main ()
Return 0;
d) Dynamic constructor
Destructor
Syntax: ~destructor_name ()
// function body
#include<iostream>
Using namespace std;
Class myclass2
Private:
Int x;
Public:
X =10;
X =x;
X = o.x;
Void show ()
{
Cout<<”\n value is “<<x;
Cout<<”object deleted”;
};
Int main ()
Myclass2 o1;
Int num;
Cin>>num;
Myclass2 o3 (o1);
O1.show ();
O2.show ();
O3.show ();
Return 0;
}
Que3. What do you mean by specification of a class? Describe the
different access specifier in class.
Classes in c++
Class is used to create user defined data types and behaves like built
in data types of programming language.
Data items in a class are called data members. The function within a
class is called member functions.[user defined class].
Example:
Class myclass {
Member variables;
………………………………..
Member functions ();
………………………………
Program:
#include<iostream>
Class book
Private:
Int book_no;
Float price;
Public:
Void input ()
Cin>>book_no;
Cin>>price;
Void show ()
};
<<endl<<”price:”<<price;
Void main ()
Book obj;
Return 0;
Que4. What are read only objects? What is the role of constructor in
creating such objects? How can you create and destroy objects
dynamically? Explain with an example.
Ans. The dynamic initialization means that the initial value may be
provided during run time. Even class objects can be initialized
dynamically i.e. using the value which are provided at run time.
Example:
Class factorial
Private:
Int n;
Public:
Factorial (int number)
N = number;
Void display ()
If (n==0)
Else
};
Int main ()
Int x;
Cout<<”enter the number to find the factorial”;
Cin>>x;
Return 0;
Read-only objects:
The new operator can be used to create objects of any type and its
general form is:
Delete pointer-variable;
#include<cstring>
Int main ()
Int num;
Cin>>num;
Float *ptr;
Cout<<”student”<<i+1<<” “;
Cin>>*(ptr+i);
Cout<<”student”<<i+1<<” “<<*(ptr+i)<<endl;
}
Delete [] ptr;
Retrun 0;
Ans. A data types is a finite set of values along with set of rules for
permissible operations.
Show in figure, two broad categories of data types are- primitive and
non-primitive. Primitive data types have further two categories –
numeric and non numeric. Numeric data types have further two
categories –integers and floating point. Character data types comes
under non-numeric category. Classes and array are two non
primitive (i.e. derived data types).
PREMITIVE NON-PREMITIVE
ARRAYS
BUILT-IN
NUMERIC NON-NUMERIC
Void- to specify the return type of the function
Format specifier - %c
Format specifier- %f
Color background;
In c programming enum is type of int but in c++, ach of different type.
C++ does not permit an int value to be automatically connected to an
enum.
Color background = 7;
In special case when class’s private data needs to be accessed directly
without using object of that class.
#include<iostream>
Using namespace std;
Class distance
Private:
Int meters;
Public:
Distance ()
Meters = 0;
Void displaydata ()
};
}
Int main ()
Distance d1;
D1.displaydata ();
Return 0;
OUTPUT
Meter value = 0
Que. What is function in c++? How will you define and call a function?
Explain with an example.
Some functions are already defined in c++ library. They are called as
library functions. These library functions can be easily be used in c++
programs, another that user can create some new functions also, which
are called user defined functions.
Its definition
Its declaration
And its calling
Main()
……….
Show()/*function call*/
……….//function body
#include<iostream>
Double volume (double w, double h, double d)// function declaration or
prototype
Width = 5.6;
Height = 3.4;
Depth = 2.3;
Double v;
V = w*h*d;
Return v;