CPP 3
CPP 3
Pointer to an object
• A pointer is a variable that stores the memory address of another
variable (or object) as its value. A pointer aims to point to a data type
which may be int, character, double, etc.
• Pointers to objects aim to make a pointer that can access the object,
not the variables. Pointer to object in C++ refers to accessing an
object.
• There are two approaches by which you can access an object. One is
directly and the other is by using a pointer to an object in C++.
• A pointer to an object in C++ is used to store the address of an object.
For creating a pointer to an object in C++, we use the following
syntax:
• classname*pointertoobject;
• For storing the address of an object into a pointer in c++, we use the
following syntax:
• pointertoobject=&objectname;
• The above syntax can be used to store the address in the pointer to
the object. After storing the address in the pointer to the object, the
member function can be called using the pointer to the object with
the help of an arrow operator.
#include <iostream>
using namespace std;
class My_Class {
int num;
public:
void set_number(int value) {num = value;}
void show_number();
};
void My_Class::show_number()
{
cout << num << "\n";
}
int main()
{
My_Class object, *p; // an object is declared and a pointer to it
return 0;
}
Base Class Pointer Pointing to Derived Class
• A pointer is a data type that stores the address of other data
types. Pointers can be used for base objects as well as objects
of derived classes. A pointer to the object of the derived class
and a pointer to the object of the base class are type-
compatible (may be used in different ways).
• The pointer of Base Class pointing different objects of the
derived class
• A derived class is a class that takes some properties from its base
class.
• It is true that a pointer of one class can point to another class, but
classes must be a base and derived class, then it is possible.
• To access the variable of the base class, a base class pointer will
be used.
• So, a pointer is a type of base class, and it can access all,
public function and variables of the base class since the pointer is of
the base class, this is known as a binding pointer.
• In this pointer base class is owned by the base class but points to
the derived class object.
• The same works with derived class pointer, values are changed.
#include <iostream>
using namespace std; int main()
// Base Class {
class BaseClass { // Pointer to base class
public: BaseClass* base_class_pointer;
int var_base; BaseClass obj_base;
void display() DerivedClass obj_derived;
{
cout << "Displaying Base class" // Pointing to derived class
<< " variable var_base: " << var_base << endl; base_class_pointer = &obj_derived;
}
}; base_class_pointer->var_base = 34;
class DerivedClass : public BaseClass { base_class_pointer->display();
public:
int var_derived; base_class_pointer->var_base = 3400;
void display() base_class_pointer->display();
{
cout << "Displaying Base class" DerivedClass* derived_class_pointer;
<< " variable var_base1: " << var_base << endl; derived_class_pointer = &obj_derived;
cout << "Displaying Derived " derived_class_pointer->var_base = 9448;
<< " class variable var_derived: " derived_class_pointer->var_derived = 98;
<< var_derived << endl; derived_class_pointer->display();
}
}; return 0;
}
Files and Streams
• In C++ programming we are using the iostream standard
library, it provides cin and cout methods for reading from
input and writing to output respectively.
• To read and write from a file we are using the standard C++
library called fstream. Let us see the data types define in
fstream library is:
Data Type Description
It is used to create files, write information to files, and read information from
fstream
files.
ifstream It is used to read information from files.
ofstream It is used to create files and write information to the files.
getline()
• The cin is an object which is used to take input from the
user but does not allow to take the input in multiple
lines. To accept the multiple lines, we use the getline()
function. It is a pre-defined function defined in
a <string.h> header file used to accept a line or a string
from the input stream until the delimiting character is
encountered.
• There are two ways of representing a function:
• The first way of declaring is to pass three parameters.
• istream& getline( istream& is, string& str, char delim );
• The second way of declaring is to pass two parameters.
• istream& getline( istream& is, string& str );
• Where,
• is: It is an object of the istream class that defines from where to read the
input stream.
• str: It is a string object in which string is stored.
• delim: It is the delimiting character.
• Return value
• This function returns the input stream object, which is passed as a
parameter to the function.
Unformatted input/output operations
• Using objects cin and cout for the input and the output of data of various types is
possible because of overloading of operator >> and << to recognize all the basic
C++ types. The operator >> is overloaded in the istream class and operator << is
overloaded in the ostream class.
• The general format for reading data from the keyboard:
• cin >> var1 >> var2 >> …. >> var_n;
• Here, var1, var2, ……, varn are the variable names that are declared already.
• The input data must be separated by white space characters and the data type
of user input must be similar to the data types of the variables which are
declared in the program.
• The operator >> reads the data character by character and assigns it to the
indicated location.
• Reading of variables terminates when white space occurs or character type
occurs that does not match the destination type.
put() and get() functions
• The class istream and ostream have predefined
functions get() and put(), to handle single character input and
output operations.
• The function get() can be used in two ways, such
as get(char*) and get(void) to fetch characters including blank
spaces, newline characters, and tab.
• The function get(char*) assigns the value to a variable.
getline() and write() functions
• In C++, the function getline() and write() provide a more efficient
way to handle line-oriented inputs and outputs. getline() function
reads the complete line of text that ends with the new line
character. This function can be invoked using the cin object.
• Syntax:
• cin.getline(variable_to_store_line, size);
• Syntax:
• cout.write(variable_to_store_line, size);
• The key point to remember is that the write() function does not
stop displaying the string automatically when a NULL
character occurs. If the size is greater than the length of the line
then, the write() function displays beyond the bound of the line.
FileStream example: writing to a file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream filestream("testout.txt");
if (filestream.is_open())
{
filestream << "Welcome to javaTpoint.\n";
filestream << "C++ Tutorial.\n";
filestream.close();
}
else cout <<"File opening is fail.";
return 0;
}
FileStream example: reading from a file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
string srg;
ifstream filestream("testout.txt");
if (filestream.is_open())
{
while ( getline (filestream,srg) )
{
cout << srg <<endl;
}
filestream.close();
}
else {
cout << "File opening is fail."<<endl;
}
return 0;
}
Read and Write Example
#include <fstream> ifstream is;
#include <iostream> string line;
using namespace std; is.open("testout.txt");
int main () { cout << "Reading from a text file:" << endl;
char input[75]; while (getline (is,line))
ofstream os; {
os.open("testout.txt"); cout << line << endl;
cout <<"Writing to a text file:" << endl; }
cout << "Please Enter your name: "; is.close();
cin.getline(input, 100); return 0;
os << input << endl; }
cout << "Please Enter your age: ";
cin >> input;
cin.ignore();
os << input << endl;
os.close();