Found 7346 Articles for C++

Catching base and derived classes exceptions in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

To catch an exception for both base and derive class then we need to put catch block of derived class before the base class. Otherwise, the catch block of derived class will never be reached.AlgorithmBegin    Declare a class B.    Declare another class D which inherits class B.    Declare an object of class D.    Try: throw derived.    Catch (D derived)       Print “Caught Derived Exception”.    Catch (B b)       Print “Caught Base Exception”. End.Here is a simple example where catch of derived class has been placed before the catch of base ... Read More

How to declare a pointer to a function in C?

Samual Sam
Updated on 30-Jul-2019 22:30:25

5K+ Views

A pointer is a variable whose value is the address of another variable or memory block, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable or block address.SyntaxDatatype *variable_nameAlgorithmBegin.    Define a function show.       Declare a variable x of the integer datatype.       Print the value of varisble x.    Declare a pointer p of the integer datatype.    Define p as the pointer to the address of show() function.    Initialize value to p pointer. End.This is a simple ... Read More

vector::resize() vs vector::reserve() in C++

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

703 Views

Vectors have the ability to resize itself automatically like dynamic arrays when an element is inserted or deleted, the container handles their storage automatically.The main difference between vector resize() and vector reserve() is that resize() is used to change the size of vector where reserve() doesn’t. reserve() is only used to store at least the number of the specified elements without having to reallocate memory. But in resize(), if the number is smaller than the current number then it resizes the memory and deletes the excess space over it.vector::resize()Vector resize() is used to change its size.ExampleSteps in the source code:Begin ... Read More

std::vector::resize() vs. std::vector::reserve() in C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

Vectors have the ability to resize itself automatically like dynamic arrays when an element is inserted or deleted, the container handle their storage automatically.The main difference between vector resize() and vector reserve() is that resize() is used to change the size of vector where reserve() doesn’t. reserve() is only used to store at least the number of the specified elements without having to reallocate memory. But in resize() if the number is smaller than the current number then it resizes the memory and deletes the excess space over it.vector::resize()Vector resize() is used to change its size.ExampleSteps in the source code:Begin ... Read More

tmpfile() function in C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

The function tmpfile() creates a temporary file in binary update mode in C. It initializes in header file of a C program. It always returns a null pointer if the temporary file cannot be created. The temporary file deleted automatically just after the termination of program.SyntaxFILE *tmpfile(void)Return valueIf file creation is successful, the function returns a stream pointer to the temporary file created. If the file cannot be created, NULL pointer is returned.AlgorithmBegin.    Declare an array variable c[] to the character datatype and take a character data string.    Initialize a integer variable i ← 0.    Declare a ... Read More

tellp() in file handling with C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

In C++ file handling, the tellp() function is used with output streams, and returns the current put position of the pointer in the stream. It returns an integer data type, representing the current position of the stream pointer.tellp() method takes no parameter. It is written as: pos_type tellp();AlgorithmBegin.    Create an object newfile against the class fstream.    Call open() method to open a file “tpoint.txt” to perform write operation using object newfile.    Insert data into the file object newfile.    Call the tellp() method to print the present position of the pointer in the file object.    Call ... Read More

Read file line by line using C++

karthikeya Boyini
Updated on 14-Sep-2023 13:43:23

25K+ Views

This is a C++ program to read file line by line.Inputtpoint.txt is having initial content as "Tutorials point."OutputTutorials point.AlgorithmBegin    Create an object newfile against the class fstream.    Call open() method to open a file “tpoint.txt” to perform write operation using object newfile.    If file is open then       Input a string “Tutorials point" in the tpoint.txt file.       Close the file object newfile using close() method.    Call open() method to open a file “tpoint.txt” to perform read operation using object newfile.    If file is open then       Declare a ... Read More

Read Data from a Text File using C++

Samual Sam
Updated on 30-Jul-2019 22:30:25

11K+ Views

This is a C++ program to read data from a text file.Inputtpoint.txt is having initial content as “Tutorials point.”OutputTutorials point.AlgorithmBegin    Create an object newfile against the class fstream.    Call open() method to open a file “tpoint.txt” to perform write operation using object newfile.    If file is open then       Input a string “Tutorials point" in the tpoint.txt file.       Close the file object newfile using close() method.    Call open() method to open a file “tpoint.txt” to perform read operation using object newfile.    If file is open then       Declare ... Read More

Merge contents of two files into a third file using C

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

848 Views

This is a c program to merge the contents of two files into the third file.For Example.Inputjava.txt is having initial content “Java is a programing language.” kotlin.txt is having initial content “ kotlin is a programing language.” ttpoint.txt is having initial content as blankOutputfiles are merged ttpoint.txt will have final content as “Java is a programing language. kotlin is a programing language.”AlgorithmBegin    Declare a[] array to the character datatype.       Initialize a[] = "Java is a programing language.".    Declare i of the integer datatype.       Initialize i =0.    Declare f1 as a pointer ... Read More

How to read a text file with C++?

Samual Sam
Updated on 07-Nov-2023 04:43:01

63K+ Views

This is a C++ program to read a text file.Inputtpoint.txt is having initial content as “Tutorials point.”OutputTutorials point.AlgorithmBegin    Create an object newfile against the class fstream.    Call open() method to open a file “tpoint.txt” to perform write operation using object newfile.    If file is open then       Input a string “Tutorials point" in the tpoint.txt file.       Close the file object newfile using close() method.    Call open() method to open a file “tpoint.txt” to perform read operation using object newfile.    If file is open then       Declare a string ... Read More

Advertisements