Unit-4-Class and Object
Unit-4-Class and Object
Definition:
In object oriented programming language the problem or program is divided into
smaller objects.
The object oriented programming language is to combine both data and functions is
known as Object.
Major Problem
3. Data Abstraction
Data Abstraction refers to the representation of essential features without
including the background details.
Data abstraction refers to the representation of needed information in the
program without presenting details.
4. Encapsulation
The packing of data and functions into a single component is known as encapsulation.
Example of Encapsulation is Class.
A class defines the structure of data and member functions.
Encapsulation is a way of achieving data security in an object-oriented program.
Object
Data
Methods
5. Inheritance
A new class can be derived from the existing class without affecting the existing
class is treated as inheritance.
In the following figure: Course is base class/Super class/parent class/Existing
class
6. Polymorphism
It is an important feature of OOP concept.
Poly means Many or Several, Morphism means Forms or behaviors.
It holds an ability to take more than one form or behavior.
In polymorphism, two types of polymorphism are involved.
o Function overloading: only one function can do different behavior.
o Operator overloading: only one operator can do different behavior.
7. Dynamic Binding
Global variables:
Global Variable section is Optional.
Those variables are working out of the function.
Global variable will hold their value throughout the lifetime of your program.
Main function:
Main section is mandatory.
Every C++ program will have one main().
This is main execution of a program.
Main function starts with opening braces{ and the closing brace }
The statements enclosed within the opening brace and the closing brace form the
body of the main().
It consists of two parts.
Local variables declaration: Those variables are working within a function
Statements: it refers to the executable statements.
Simple Program:
Use text editors such as Gedit, Kwrite, Medit, ed, VI, etc.
Windows platform:
Create a source program using integrated Turbo C++ editor. Go to the File menu and
select New. The file name NONAME00.CPP appears on the screen at the center of
the window. Check whether your source program is typed correctly. If mistakes are
found, correct them.
STEP 3: SAVE THE FILE WITH .CPP AS AN EXTENSION
Now press F2 to save your program or use Save option with .cpp extension.
You can change the file name and save it using the Save option in File menu.
In the following window the file is saved with ABC.CPP.
STEP 4: COMPILE THE PROGRAM
Windows platform:
From the File menu, open the program file and compile it using Compile option in
Compile menu or use Alt+F9.
It will display the errors and warnings. If there are any errors or warnings, correct
them and save the corrected file again, compile again. If program is correct, then
the window shows:
Warnings: 0
Errors: 0
STEP 5: RUN THE PROGRAM
Windows platform:
Use‘Run’option from Run menu of Turbo C++ IDE or use ctrl+F9 and see the output
as per program.
The following window shows execution of ABC.CPP program.
4. Describe about Input and Output statements In C++?
4. write about Cin and Cout statements in C++?
The Input and Output Statements (cin and cout)
The cin Statement
The cin is the input statement.
The statement is used to receive a value from the keyboard and stores in a
variable.
Syntax:
cin >> variable;
The symbol >> is called the insertion operator.
Member data:
Member functions:
Member data:
Access specifiers define how the members (attributes and methods) of a class can
be accessed.
In C++, there are three access specifiers:
Example:
To access the class members we can use the dot syntax (.) on the object:
Dot is the access operator.
Example
}
Output:
15
5000.0
7. Explain the static member data and static member
function in C++?
Static Member Data in c++
Definition:
It is variable in the class that is called member data or member variable.
It is declared with static keyword so this is called static member data or variable.
It provides single copy of the variables for the all objects.
When we declare a normal variable (data member) in a class, different copies of
those data members create with the associated objects.
Static member function can access only static data members or functions.
Static member function can be invoked using class name.
It is also possible to invoke static member functions using objects.
Declaration syntax for static member functions:
Statements;
Example:
Write a program in c++ to show the static member data and static member
functions?
#include<iostream.h>
public:
++c;
cout<<“\n c=”<<c;
};
int number :: c=0; // initialization of static member variable
getch();
return 0;
OUTPUT
c=1
c=2
c=3
The function is declared with friend keyword that function is called friend
function.
Friend function is used to access the private and protected data of class.
For accessing the data, the declaration of a friend function should be done inside
the body of a class starting with the keyword friend.
Statements;
Example:
Write a program to access private data using non-member function Use friend
function.
#include <iostream.h>
class temp //Class name
{
private: //Access Specifier
int x; //member data/variable
public: //Access Specifier
void get() //member function
{
cin>>x;
}
friend void display(temp &t) //member function with friend
{
cout<<t.x;
}
};
int main() //main function
{
temp y; //Object creation
cout << "Enter the value of t \n";
y.get();
cout << "The value of t = ";
display(y);
getch();
return 0;
}
9. What is meant by Constructor? Narrate the features of
Constructors? Types of constructors in C ++?
Definition:
A constructor is defined to be a special member function.
It is used to initialize the class variables within a class.
The constructor has the same name as the class.
It is always public, and it does not have any return value.
Characteristics:
It should be a public member.
The name of the constructor should be as that of the class name.
It can take arguments (Even the default arguments) but it does not return
any value.
No return type should be specified including void.
It is invoked implicitly at the time of creation of the objects.
The addresses of the constructors cannot be retrieved.
Types of Constructors
1. Default constructor
2. Parameterized constructor
3. Copy constructor
4. Dynamic constructor.
1. Default constructor:
Default constructor is the constructor.
It is a special member function.
It doesn’t take any argument. So this is called Default Constructor.
It will not return any value.
Syntax of Default constructor:
Class name ( )
{
Statements;
}
Example:
Program: Default Constructor in Integer Class
#include <iostream.h>
#include<conio.h>
class integer //class name
{
private: //Access specifier
int x; //member data
public: //Access specifier
integer( ) // Default Constructor
{
x = 10;
}
void display( ) //member function
{
cout << x << "\n";
}
};
int main() //main function
{
clrscr();
integer a, b; //objects creating
cout << "The member data of a =";
a.display( );
cout << "The member data of b =";
b.display( );
getch();
return 0;
}
Input-Output:
3. Copy Constructor
Copy constructor is constructor.
It is special member function.
A constructor is used to initializes an object with another object is called the
copy constructor.
Syntax:
Class name (class name &variable name)
{
Statements;
}
Example:
#include<iostream.h>
class num
{
int n;
public:
{
n=j.n;
}
void show()
{
cout<<n<<”\n”
}
};
main()
{
num J(50); //object with parameter
num K(J); //j object is copied into k object
J.show();
K.show();
getch();
return 0;
}
4. Dynamic Constructor:
A constructor is used allocates memory and initializes an object during
runtime is called a dynamic constructor.
9. Define Destructors and its Characteristics?
Destructor:
A destructor works opposite to constructor.
Destructor is a member function which destructs or deletes an object.
It can be defined only once in a class. Like constructors, it is invoked
automatically.
A destructor is defined like constructor.
It must have same name as class. But it is prefixed with a tilde sign (~).
Characteristics of a destructor:
It should be a public member function..
The name of the destructor should be as that of the class name and it is
preceded by the tilde symbol ~.
It cannot take arguments and does not return any value.
No return type should be specified including void.
Example:
#include<iostream.h>
#include<conio.h>
class Employee //class structure
{
public: //Access specifier
Employee() //Constructor
{
cout<<”Constructor invoked”;
}
~Employee() //Destructor
{
cout<<”Destructor invoked”;
}
};
Destructor invoked