C++ Object and Class

C++ Class

A class is the collection of related data and function under a single name. A C++ program can have any number of classes.

Class is defined in C++ programming using keyword class followed by identifier(name of class). Body of class is defined inside curly brackets an terminated by semicolon at the end in similar way as structure.

class class_name
   {
   // some data
   // some functions
   };

C++ Object

When class is defined, only specification for the object is defined. Object has same relationship to class as variable has with the data type. Objects can be defined in similary way as structure is defined.

class_name variable name;

temp obj1,obj2;

Accessing Data Members and Member functions

Data members and member functions can be accessed in similar way the member of structure is accessed using member operator(.).

For the class and object defined above, func1() for object obj2 can be called using code:

obj2.func1();

Similary, the data member can be accessed as:

object_name.data_memeber;

Constructor

Constructors are the special type of member function that initialises the object automatically when it is created .

Constructor has same name as that of class and it does not have any return type.

class A
{
 int x;
 public:
 A();  //Constructor
};

Destructor
Destructor is a special class function which destroys the object as soon as the scope of object ends. The destructor is called automatically by the compiler when the object goes out of scope.
The syntax for destructor is same as that for the constructor, the class name is used for the name of destructor, with a tilde ~ sign as prefix to it.
class A
{
 public:
 ~A();
};



File Handling in C++

File Handling concept in C++ language is used for store a data permanently in computer. Using file handling we can store our data in Secondary memory (Hard disk).
Why use File Handling

  • For permanet storage.
  • The transfer of input - data or output - data from one computer to another can be easily done by using files.
  • For read and write from a file you need another standard C++ library called fstream, which defines three new data types:

Datatype
Description
ofstream
This is used to create a file and write data on files
ifstream
This is used to read data from files
fstream
This is used to both read and write data from/to files

Defining and Opening a File
The function open() can be used to open multiple files that use the same stream object.

file-stream-class   stream-object;
stream-object.open("filename");

Closing a File

A file must be close after completion of all operation related to file. For closing file we needclose() function.

outfile.close();

 put() and get() function

The function put() write a single character to the associated stream. Similarly, the function get() reads a single character from the associated stream.
read() and write() function
These function take two arguments. The first is the address of the variable V , and the second is the length of that variable in bytes. The address of variable must be cast to type char * (i.e pointer to character type).


file . read ((char *)&V , sizeof (V));
file . Write ((
char *)&V , sizeof (V));

Menu Parser in C++ 



 

C++ Object and Class C++ Object and Class Reviewed by Unknown on 05:19:00 Rating: 5

No comments: