[go: up one dir, main page]

0% found this document useful (0 votes)
55 views3 pages

C++ Important Questions Answers

The document outlines important C++ concepts, including the differences between class and function templates, the need for template functions, and the use of user-defined types in templates. It also covers I/O operations, manipulators, file handling, and the Standard Template Library (STL). Key topics include formatted vs unformatted I/O, object serialization, and the types of containers available in C++.

Uploaded by

BAVESSH Chaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views3 pages

C++ Important Questions Answers

The document outlines important C++ concepts, including the differences between class and function templates, the need for template functions, and the use of user-defined types in templates. It also covers I/O operations, manipulators, file handling, and the Standard Template Library (STL). Key topics include formatted vs unformatted I/O, object serialization, and the types of containers available in C++.

Uploaded by

BAVESSH Chaman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C++ Important Questions - Answers

1. Class templates vs Function templates:

Class templates define blueprints for classes with generic types, while function templates define generic

functions.

Example:

template<class T> class MyClass { T data; };

template<class T> T add(T a, T b) { return a + b; }

2. Need for template functions:

They avoid code duplication and allow operations with any data type.

3. User-defined types in templates:

Templates can use custom structs/classes.

Example:

template<typename T> class Stack { T arr[100]; };

4. Default arguments in class templates:

template<class T=int> class Example { T data; };

5. Typename vs class:

In templates, both are interchangeable, but 'typename' is also used to specify dependent types.

6. Formatted vs Unformatted I/O:

Formatted uses manipulators like setw; Unformatted uses low-level functions like get(), put().

7. C++ Stream Hierarchy:

ios

istream ifstream

ostream ofstream

iostream fstream
8. Purpose of setprecision():

Sets the number of digits displayed after the decimal point.

9. Clear all formatting flags:

Use cout.setf(0, ios::floatfield);

10. Template for reusability:

template<typename T> T max(T a, T b) { return (a > b) ? a : b; }

11. Three I/O manipulators:

setw(), setprecision(), setfill()

12. Manipulators vs ios functions:

Manipulators modify stream settings, ios functions control stream behavior.

13. Text vs Binary files:

Text files are human-readable; binary files store raw data.

14. Purpose of seekg() and seekp():

seekg(): move get pointer; seekp(): move put pointer.

15. put/get vs getline/write:

put/get are character-level; getline/write are line/block level.

16. Object serialization:

Saving object state using write()/read().

17. getline() vs cin:

geline reads full line including spaces; cin stops at space.

18. Opening/writing binary file:

ofstream fout("data.bin", ios::binary);

fout.write((char*)&obj, sizeof(obj));

19. Iterators vs Functors:


Iterators access container elements; functors are objects used as functions.

20. STL vs Other Libraries:

STL is built-in and generic; other libraries like Boost offer specialized tools.

21. Store objects in files:

ofstream fout("file.dat", ios::binary);

fout.write((char*)&obj, sizeof(obj));

22. Types of containers:

Sequence (vector, list), Associative (map, set), Unordered (unordered_map)

23. Random access in file:

Use seekg() or seekp() to jump to any file position.

You might also like