DAY 2 Class MemberFunction Constructor
DAY 2 Class MemberFunction Constructor
DAY 2 Class MemberFunction Constructor
Day-2:
Basic I/O, Class and objects, Class member
functions, Class access modifiers;
Constructor and destructor, Dynamic Constructor
2 <iomanip>
This file declares services useful for performing formatted I/O with so-called parameterized stream
manipulators, such as setw and setprecision.
3 <fstream>
This file declares services for user-controlled file processing. We will discuss about it in detail in File
and Stream related chapter.
I/O Examples:
Input Stream:
Output Stream:
The predefined object cin is an instance of istream class.
The predefined object cout is an instance
The cin object is said to be attached to the standard input
of ostream class. The cout object is said to be "connected
device, which usually is the keyboard. The cin is used in
to" the standard output device, which usually is the display
conjunction with the stream extraction operator, which is
screen. The cout is used in conjunction with the stream
written as >> which are two greater than signs as shown in
insertion operator, which is written as << which are two less
the following example.
than signs as shown in the following example.
#include <iostream>
using namespace std;
#include <iostream>
int main(){
using namespace std;
char name[50];
cout << "Please enter your name: "; cin >>
int main() { name;
char str[] = "Hello C++"; cout << "Your name is: " << name << endl;
cout << "Value of str is : " << str << endl; }
}
When the above code is compiled and executed, it will prompt
When the above code is compiled and executed, it you to enter a name.
produces the following result − Please enter your name: cplusplus
Value of str is : Hello C++ Your name is: cplusplus
I/O Examples:
int main() {
char str[] = "Unable to read....";
cerr << "Error message : " << str << endl;
I/O Examples:
int main() {
char str[] = "Unable to read....";
clog << "Error message : " << str << endl;
C++ Classes/Objects
Explaination:
• A member function of a class is a function that has its definition or its prototype within the class definition like any
other variable.
• It operates on any object of the class of which it is a member, and has access to all the members of a class for that
object.
Let us take previously defined class to access the members of the class using a member function instead of directly
accessing them −
class Box
{
public: double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void); // Returns box volume
};
Member Functions
Member functions can be defined within the class definition or separately using scope resolution operator, : −.
Defining a member function within the class definition declares the function inline, even if you do not use the inline
specifier. So either you can define Volume() function as below
class Box
{
public: double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void); // Returns box volume
double getVolume(void){
return length * breath * height
}
};
Member Functions
you can define the same function outside the class using the scope resolution operator (::) as follows
class Box
{
public: double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
double getVolume(void); // Returns box volume
};
Here, only important point is that you would have to use class name just before :: operator.
Object and Function Calling
A member function will be called using a dot operator (.) on a object where it will manipulate data related to that object only as
follows −
A class is used to specify the form of an object and it combines data representation and methods for manipulating that
data into one neat package. The data and functions within a class are called members of the class.
A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of
curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined
the Box data type using the keyword class as follows
Let us take previously defined class to access the members of the class using a member function instead of directly accessing them −
class Box
{
public: // keyword public determines the access attributes of the members of the class that follows it.
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
• A class provides the blueprints for objects, so basically an object is created from a class.
We declare objects of a class with exactly the same sort of declaration that we declare
variables of basic types. Following statements declare two objects of class Box −
Both of the objects Box1 and Box2 will have their own copy of data members.
Accessing the Data Members
• The public data members of objects of a class can be accessed using the direct member access operator (.).
Let us try the following example to make the things clear −
#include<iostream>
class Box
{
public: // keyword public determines the access attributes of the members of the class that follows it.
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
// box 1 specification
int main() { Box1.height = 5.0;
Box Box1; // Declare Box1 of type Box Box1.length = 6.0;
Box Box2; // Declare Box2 of type Box Box1.breadth = 7.0;
double volume = 0.0; // Store the volume of a box here
Accessing the Data Members
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0; // volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl; // volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
Output:
Volume of Box1 : 210
Volume of Box2 : 1560
Class Access Modifiers in C++
4 Copy Constructor The copy constructor is a constructor which creates an object by initializing it with an object of
the same class, which has been created previously.
5 Friend Functions A friend function is permitted full access to private and protected members of a class.
6 Inline Functions With an inline function, the compiler tries to expand the code in the body of the function in place
of a call to the function.
7 this Pointer Every object has a special pointer this which points to the object itself.
8 Pointer to C++ Classes A pointer to a class is done exactly the same way a pointer to a structure is. In fact a class is
really just a structure with functions in it.
9 Static Members of a Class Both data members and function members of a class can be declared as static.
Constructor
Constructor:
A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is
automatically called when object(instance of class) create. It is special member function of the class.
• The Compiler calls the Constructor whenever an object is created.
• It is special member function of the class.
• Constructors initialize values to object members after storage is allocated to the object.
• Compiler identifies a given member function is a constructor by its name and the return type.
• Constructors are always public.
Difference between constructor and member function
•Constructor name must be same as class name but functions cannot have same name as class name.
•Constructors does not have return type whereas functions must have return type.
•Constructors is automatically called when an object is created.
•Member function can be virtual, but there is no concept of virtual constructors.
•Constructors are invoked at the time of object creation automatically and cannot be called explicitly using class
objects.
Constructor
Constructor
int main()
{ // Default constructor called automatically when the object is
created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
Constructor
#include "iostream"
Copy Constructor in C++ using namespace std;
•A copy constructor is a member function which initializes an object using class point{
another object of the same class. private:
•Whenever we define one or more non-default constructors( with
double x, y;
parameters ) for a class, a default constructor( without parameters ) should
public: // Non-default Constructor & default Constructor
also be explicitly defined as the compiler will not provide a default
constructor in this case. point (double px, double py)
•An object can be initialized with another object of same type. This is same {
as copying the contents of a class to another class. x = px, y = py;
}
};
int main(void)
{// Define an array of size 10 & of type point. This line will
cause error
point a[10];
Syntax:
~constructor-name();
Properties of Destructor:
•Destructor function is automatically invoked when the objects are destroyed.
•It cannot be declared static or const.
•The destructor does not have arguments.
•It has no return type not even void.
•An object of a class with a Destructor cannot become a member of the union.
•A destructor should be declared in the public section of the class.
•The programmer cannot access the address of destructor.
Destructor
public:
String(char*); // constructor
~String(); // destructor
};
String::String(char* c)
{
size = strlen(c);
s = new char[size + 1];
strcpy(s, c);
}
String::~String() { delete[] s; }
Thank You