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

C++ Class Programs and Solutions

C++ progtam

Uploaded by

naikadinath738
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)
56 views3 pages

C++ Class Programs and Solutions

C++ progtam

Uploaded by

naikadinath738
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

Important C++ Programs for Final Exam

1. Write a C++ program to accept array of five elements, find and display
smallest number from an array.
2. Write a C++ program to find and display the smallest number of an array.
3. Write a program to sort an 1-d array in ascending order.
4. Write a ‘C++’ program to find factorial of given number using loop.
5. Write a C++ program to find whether the entered number is even or odd.
6. Write a program to print first n natural numbers and their sum using for loop.
7. Write a C++ program to add two 3 × 3 matrices and display addition.
8. Write a C++ program to declare a structure employee with members as empid
and empname. Accept and display data for one employee using structure
variable.
9. Write a program to declare a class ‘student’ having data members as
‘stud_name’ and ‘roll_no’. Accept and display this data for 5 students.
10. Write a C++ program to find the area of rectangle using class rectangle which
has following details – i) Accept length and breadth from the user.
ii) Calculate the area
iii) Display the result.
11. Write a C++ program to declare a structure book with members as book id and
name. Accept and display data of five books using array of structure.
12. Write a program to declare a class ‘book’ containing data members as ‘title’,
‘author-name’, ‘publication’, ‘price’. Accept and display the information for
one object using pointer to that object.
13. Write a program to declare a class ‘item’ containing data members as
‘item_name’, ‘code’, ‘price’. Accept and display the information for one object
using pointer to that object.
14. Write a program to declare a class ‘employee’ containing data members ‘emp-
id’ and ‘salary’. Accept and display this data for 10 employees.
15. Write a C++ program to declare a class COLLEGE with members as college
code. Derive a new class as STUDENT with members as studid. Accept and
display details of student along with college for one object of student.
16. Write a C++ program to declare a class college with name and code. Derive a
new class as student with members as name. Accept and display details of one
student along with college data.
17. Write a C++ program to declare a class mobile having data members as price
and model number. Accept and display the data for Ten objects.
18. Write a C++ program to declare a class ‘College’ with data members as name
and college code. Derive a new class ‘student’ from the class college with data
members as sname and roll no. Accept and display details of one student with
college data.
19. Write a C++ program to declare a class ‘Account’ with data members as accno,
name and bal. Accept data for eight accounts and display details of accounts
having balance less than 10,000.
20. Write a C++ program to declare a class ‘circle’ with data members as radius
and area. Declare a function getdata to accept radius and putdata to calculate
and display area of circle.
21. Write a C++ program to declare a class addition with data members as x and y.
Initialize value of x and y with constructor. Calculate addition and display it
using function ‘display.’
22. Write a C++ program to declare a class student with members as roll no, name
and department. Declare a parameterised constructor with default value for
department as ‘CO’ to initialize members of object. Initialize and display data
for two students.
23. Write a C++ program to declare a class train with members as train no and
name. Accept and display data for one object of train. Use pointer to object to
call functions of class
24. Write a C++ program to print multiplication table of 7. (example: 7 × 1 = 7 .....
7 × 10 = 70)
25. Write a C++ program to swap two integer numbers and swap two float
numbers using function overloading. (Hint : overload swap function)
26. Write a C++ program to find smallest number from two numbers using friend
function. (Hint : use two classes).
27. Write a C++ program to declare two classes with data members as m1 and m2
respectively. Use friend function to calculate average of two (m1, m2) marks
and display it.
28. Write a C++ program to overload add function to add two integer numbers and
two float numbers.
29. Write a C++ program to count number of spaces present in contents of file.
30. Write a program to count the number of lines in file.
31. Write a C++ program to write ‘Welcome to poly’ in a file. Then read the data
from file and display it on screen.
32. Write a C++ program to append data from abc .txt to xyz .txt file.
33. Write a program that copies contents of one file into another file.
34. Write a C++ program to find greatest number among two numbers from two
different classes using friend function.
35. Write a C++ program to overload binary operator ‘+’ to concatenate two
strings.
36. Write a program to swap two integers using call by reference method.
37. Write a program to overload the ‘—’ unary operator to negate the values.

Note : Remaining all programs from Inheritance Chapter which are in


your Homework Book.

Common questions

Powered by AI

In C++, a friend function can be used to enable interaction between two classes by granting the function access to the private members of the classes. Define a friend function outside both classes, providing it access to specific private data members. This approach is used, for example, to find the smallest number between objects of two different classes, by making the function a friend of both classes .

Pointers make classes and objects in C++ more dynamic by allowing dynamic memory allocation and deallocation, as well as efficient object interaction. Implementing a 'Mobile' class with members like 'price' and 'model number', you can define methods that utilize pointers to create and delete objects dynamically and to access object methods using these pointers. This seminal approach improves memory management and program efficiency .

Operator overloading in C++ allows developers to define custom behaviors for operators, enhancing functionality. For example, overloading the '+' operator enables concatenation of two strings just like arithmetic addition, optimizing code readability and efficiency. This facility can also be applied to arithmetic operations to extend their logical behavior across custom data types .

Inheritance in C++ allows subclasses to inherit characteristics and behaviors from parent classes, enhancing code reusability and structure. For example, a 'College' class with 'name' and 'code' can be a parent class, and a 'Student' class can inherit from it, adding specific members like 'sname' and 'roll no'. This reuses code defining common traits and behaviors while extending functionality for student-specific needs .

Structures in C++ are simpler data models compared to classes, ideal for managing plain data without complex behaviors. They are beneficial for lower memory overhead and straightforward syntax, as seen in managing employee data. However, they lack access control and functional behaviors, which classes offer, reducing encapsulation and reusability .

To design a C++ program to display the smallest number in an array of integers, you can loop through the array while keeping track of the smallest number found. Initiate a variable, say 'smallest', with the first element of the array. Then iterate through each element, updating 'smallest' if a smaller element is found. This approach efficiently finds the smallest number in O(n) time complexity .

File handling in C++ involves using file stream classes like fstream, ifstream, and ofstream. By opening files in the appropriate mode (e.g., read, write), you can execute operations such as copying data from one file to another or appending data from a source file to a target file. This is achieved via methods like 'open', 'close', and stream insertion (<<) and extraction (>>) operators, facilitating flexible data manipulation .

To effectively manage book information and handle output operations using pointers, declare a class 'Book' with data members such as 'title', 'author-name', 'publication', and 'price'. Create an instance of the class and use a class pointer to initialize and access the instance's member functions, which will accept book details and handle output operations. Using pointers allows for dynamic interaction with objects and efficient memory utilization .

You should use function overloading to handle swaps of both integer and float types. Define separate functions named 'swap' for each data type, so one function will swap two integers and the other will swap two floats. Overloaded functions are distinguished by their parameter lists, allowing C++ to call the appropriate function based on argument types .

When designing a program for 3x3 matrix addition in C++, it's essential to define two 3x3 arrays and initialize them with input values. Iterate through each matrix element, adding corresponding elements and storing results in a third matrix. Ensure boundary checks and type safety in input handling to prevent array overflows and maintain program stability and correctness .

You might also like