Chapter7 8 9
Chapter7 8 9
POINTERS
Pointers:are the variables which are used to hold the memory address of the other variable.
int*a;
int*ptr;
float*b;
ptr=&a;
cout<<ptr;
it will show the address of a
De-reference operator(*):
If we want to store the values of the variable through the pointer then we will use a special
type of operator calledde-reference operator denoted by(*).
Example:
#include<iostream>
main()
{
int a;
a=10;
int*ptr;
1
p=&a;
cout<<p; //it will show the address of a.
cout<<*p; //it will show the value of a.
}
To declare a pointer variable is simple & similar to the declaration of simple variable with a
minor difference of theuse of (*).
Void pointer:
As we know that a pointer of type int can only points to a variable of type int.It cannot point
to some other data type variable so the data type of the variable & data type of the pointer will be
same, but there isthe special type of pointer calledvoid pointerwhich can point to any data type.
Its declaration is similar to a normal pointer with the only difference of using a keyword
void.
Syntax:
void*ptr;
Example:
#include<iostream>
main()
{
int x;
float y;
void*ptr;
ptr=&x;
ptr=&y;
}
2
Q. What is pointer initialization? Explain the concept with simple program.
Pointer initialization:
3
CHAPTER: 8
OBJECTS AND CLASSES
Class:
4
Q. What is member of Classes?
Class has 2 members
i. Data members
ii. Member functions
i. Data members:
The variable inside the class are datamembers. We can also say that attributes of class are
called data members.
Mostly data members are declare under the private access specifier.
It can also be used under public access specifier.
The following line of code explain the data members:
class car
{
private:
int door;
int wheel;
};
5
Q. What is access specifier? Explain public & private specifier with example.
Access specifier:
Access specifier in C++ defines how the members of class can be access.
They determines that which member of class is accessible in which part of program or class.
The most commonly used access specifier in C++ are:
i. Private access specifier
ii. Public access specifier
6
ii. Public access specifier:
Public members are accessible accessible from anywhere, within the class, derive class &
main function.
Example:
class temp
{
public:
int x=10;
public:
show()
{
cout<<x;
}
};
main()
{
temp t;
t.show();
}
7
The following table will show the level of hiding the members of classes.
class student
{
public:
student()
{
cout<<"I am in constructor";
}
public:
show()
{
cout<<"I am in show function";
}
};
main ()
{
Studentstd;
}
Constructor overloading:
8
Using more than one constructor in the same class is called constructor overloading.
Example:
class temp
{
public:
temp()
{
cout<<"I am in temp constructor1";
}
public:
temp(int a)
{
cout<<"value is"<<a;
}
};
main()
{
temp t1,t2(20);
}
Destructors:
They are special member functions that are executed when an object is destroyed.
They are called automatically when objects are destroyed.
They are denoted by (~) tilde sign.
Features:
i. They have same name as that of class name preceded by(~)tilde symbol.
ii. They cannot take arguments.
iii. It has no return type.
Example:
class student
{
public:
student()
{
cout<<"I am in constructor";
}
public: ~student()
9
{
cout<<"I am in destructor";
}
};
main()
{
studentstd;
}
#include<iostream>
using namespace std;
{
class my class
{
private:
inta,b,c,sum;
public:
sumfun();
{
cin>>a>>b>>c;
sum=a+b+c;
cout<<"the sum is="<<sum;
}
};
main()
{
my classc1
c1.sumfun();
}
}
10
Object oriented programming languages have a key features inheritance & polymorphism.
Inheritance:
When new classes are created from existing classes is called inheritance.
It use the concept of parent & child class.
A (parent class) is a class from which other classes are derived.
It is also called base class.
A sub-class is a class which inherit features from base class
It is also called child class or derived class.
Example:
#include<iostream>
using namespace std;
{
class A
{
public:
show a()
{
cout<<"I am in base class A ";
}
};
class B: public A
{
public:
show b()
{
cout<<"I am in child class";
}
};
main()
{
B b1;
b1.show a();
b1.show b();
}
}
Polymorphism:
It is the ability to use an operator or function in multiple ways.
11
It gives different meanings to the operators or function.
Example:
A person can be a student & a friend to another person.
In C++ it can be achieve by one of the following concept:
i. Functions overloading
ii. Operators overloading
iii. Virtual function
Virtual Function:
It is a function whose behavior can be overridden within a inheriting class by a function with
same signature.
This concept is very important part of polymorphism in object oriented programming.
Chapter No .9
File Handling in C++
What is file?
12
A file is a collection of bytes stored on storage device like hard disk.
File handling concept in C++ language is use for storing data permanently in computer.
It provides a mechanism to write output of a program into a file and from a file.
TYPES OF FILES
C++ divides files into two different types based on how they store data.
1. Text files.
2. Binary files.
TEXT FILE:
1. Text files are the stream of characters that computer can process sequentially.
2. It is not only processed sequentially but also in forward direction.
3. For this reason, text files are usually opened for only one kind of operation (reading, writing,
appending) at any given time.
4. Similarly, text files only process characters they can only read and write data one character
at a time.
5. In C++ programming language, functions are provided that deal with the lines of text.
BINARY FILES:
13
In C++ programming language, processing a file using random access techniques involves moving
the current file position to an appropriate file before reading and writing data.For example,
database files will be created and processed as a binary file.
OPENING A FILE:
myfile.open(filename);
Here myfileis a variable which handle the file whose name is written in parenthesis.
Ifstreammyfile;
It is use to declare variable or we can say to declare variable myfilewe use the following statement.
The dot operator is used between the variable myfile and open function.
While opening a file we tell the computer what we want to do with the file.
In order to open a file in any desire of mode the function arguments along with the file name.
Here file name representing of the file to be open and mode is an optional parameter with the
combination of following flags:
MODE DESCRIPTION
ios::in Open for input operation (reading a file).
ios::out Open for output operation (writing a file).
ios::binary Open in binary mode.
ios::ate Open a file for output and move the read/write control to the end of the
14
file.
ios::app Append mode. All output to that file to be appended to the end.
ios::trunc If the file opened for output operations, its existing contents are deleted
and replaced by new one.
CLASS MODE
ofstream ios::out
ifstream ios::in
fstream ios::in/ios::out
For ifstream and ofstream classes ios::in and ios::out are automatically and respectively
assumed, even if modes that does not include them is passed as second argument to open
function.
Write a program which create a file example.txt and write a line of text in it.
#include<iostream>
#include<fstream>
using namespace std;
main()
ofstream myfile;
myfile.open(“example.txt”);
myfile.close();
Explanation:
This code create a file called”example.txt” and write a sentence “welcome to file handling” in it
It shows the output of above program in form of text file created in a same working directory.
15
READING THE FILE:
To read a word from the file, we can write as myfile>>c;So the first word of the file will be read in c,
where c is a character array.
(ii) It means that when a space will come, it will stop reading further.
The first character will be read in c1, second in c2 and the third in c3.
Write a program that input file example.txt which read and display the text on screen?
#include<iostream>
#include<fstream>
using namespace std;
main()
{
Ifstream myfile;
Char ch[100];
myfile.open(“example.txt”);
myfile>>ch;
cout<<ch;
myfile.close();
bof() function:
16
1) bof stands for beginning of file.
2) It is used to set the pointer at the beginning of the file.
3) It returns true if the current position of a pointer is at the beginning of a file otherwise it
returns false.
4) It means that it tells the compiler whether the cursor is at the beginning of file or not.
eof() function:
Q Write a program which reads data from a file by using eof() function?
#include<iostream>
#include<fstream>
17
using namespace std;
main()
Ifstream myfile;
charch[100];
myfile.open(“example.txt”);
While(! myfile.eof())
myfile>>ch;
cout<<ch<<endl;
myfile.close();
1)Input stream
2) Output stream
Input stream
Input stream takes any sequence of bytes from an input device such as keyboard,
file, and network.
Output stream
Output streams are used to hold output for a particular data consumer such as
moniter, printer etc.
18
Reading and writing in a file:
1) If he is only used for reading purpose then header file “if stream” is used (input file
stream stream).
2) Similarly, if we want to write some file then header file “of stream” (output file stream)
is used.
3) One can reads, writes,manipulate the same file by using header file “f stream”.
Types of stream
The data can be read from and return to files with the help of following streams:
i) Single character stream
ii) String stream
1) Using single character stream the data can be read from and written to files character by
character.
2) The function get() is used to read data character by character.
Q: write a program which reads the data one character at a time from the file “example.txt”?
#include<iostream>
#include<fstream>
using namespace std;
main()
{
charch;
ifstream myfile(“example.txt”);
while(!myfile.eof())
{
myfile.get(ch);
cout<<ch;
}
myfile.close();
}
Program:
#include<iostream>
#include<fstream>
using namespace std;
main()
{
charch;
int a;
ofstream myfile(“example.txt”);
for (a=1;a<=20;a++)
{
cin>>ch;
cout<<myfile.put(ch);
}
myfile.close();
}
String stream:
1) A string stream is a stream which reads input from or writes output to an associated
stream.
2) Now consider the following txt file “mystring.txt” which reads the file by using get line
function and display the result on screen.
Program:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
main()
{
charch[20];
ifstreammyfile(“example.txt”);
while (!myfile.eof())
{
myfile.getline(ch,20);
cout<<ch;
}
myfile.close(); }
20