Found 7347 Articles for C++

C++ program to add different items with class templates

Arnab Chakraborty
Updated on 07-Oct-2021 08:34:01

592 Views

Suppose we want to make a class that can add two integers, two floats and two strings (string addition is basically concatenating strings). As input at first we take a number n represents there are n different operations. In each operation the first item is the type [int, float, string] and second and third are two operands. So each line will contain three elements. We shall have to read them and do the operations as mentioned.So, if the input is like5 int 5 7 int 6 9 float 5.25 9.63 string hello world string love C++then the output will be12 ... Read More

C++ program to create one rectangle class and calculate its area

Arnab Chakraborty
Updated on 07-Oct-2021 08:32:01

24K+ Views

Suppose we have taken length and breadth of two rectangles, and we want to calculate their area using class. So we can make a class called Rectangle with two attributes l and b for length and breadth respectively. And define another function called area() to calculate area of that rectangle.So, if the input is like (10, 9), (8, 6), then the output will be 90 and 48 as the length and breadth of first rectangle is 10 and 9, so area is 10 * 9 = 90, and for the second one, the length and breadth is 8 and 6, ... Read More

C++ program to test inheritance through triangle class

Arnab Chakraborty
Updated on 07-Oct-2021 08:29:55

344 Views

Suppose we want to make one Triangle class and another child class called Isosceles. The triangle class has a function that prints that the object is of type triangle, and Isosceles has two functions to show that it is an isosceles triangle and one description. We also need to call the parent class function through Isosceles class object. There is no such proper input, we just call functions in proper way.So, if the input is like define an object called trg, then call trg.isosceles(), trg.description(), trg.triangle()., then the output will beThis is an isosceles triangleThere are two sides are equal ... Read More

C++ program to store student roll and name using map STL

Arnab Chakraborty
Updated on 07-Oct-2021 08:27:18

863 Views

Suppose we have a map data structure for students roll and name the roll is an integer data and name is string type data. In our standard input we provide n queries. In each query (in each line) there must be two elements and for type 1 query there are three elements. First one is the operator, second one is the roll and third one is the name, for two elements query the second item is the roll number. The operations are like below−Insert. This will insert the name into the map at corresponding rollDelete. This will delete the against ... Read More

C++ program to insert delete and find from set STL

Arnab Chakraborty
Updated on 07-Oct-2021 08:24:11

435 Views

Suppose we have a set data structure for integer type data. In our standard input we provide n queries. In each query (in each line) we have two elements. First one is the operator, second one is the element. The operations are like below −Insert. This will insert the element into the setDelete. This will delete the element from the set (if exists)Search. This will search the element into the set, if present show Yes, otherwise No.So, if the input is like n = 7, queries = [[1, 5], [1, 8], [1, 3], [2, 8], [1, 9], [3, 8], [3, ... Read More

C++ Program to remove items from a given vector

Arnab Chakraborty
Updated on 07-Oct-2021 08:21:08

10K+ Views

Suppose we have a set of elements present inside a vector. We shall have to perform some remove operation using erase() function of vector class type to remove using indices, and finally display rest of elements. The erase function does not take the index directly. We shall have to pass its address by passing v.begin()+index, here v is the vector and v.begin() is the address of the first element (0th element). Now by adding index with it, it will move towards the element that is present at given index.So, if the input is like v = [5, 8, 6, 3, ... Read More

C++ program to create boxes and calculate volume and check using less than operator

Arnab Chakraborty
Updated on 07-Oct-2021 08:08:29

2K+ Views

Suppose we shall have to define a box class with few conditions. These are as follows −There are three attributes l, b and h for length, breadth and height respectively, (these are private variables)Define one non-parameterized constructor to set l, b, h to 0, and one parameterized constructor to set values initially.Define getter methods for each attributeDefine a function calculateVolume() get the volume of the boxOverload less than operator (

C++ program to check how many students have greater score than first one

Arnab Chakraborty
Updated on 07-Oct-2021 07:46:48

670 Views

Suppose we have n students score on five subjects. The first scores are for Kamal, and there are n-1 more scores for other students and each student has five subjects. We shall have to count number of students who have scored more than Kamal. Here we shall define one class called students to load scores for each students. The class has one Input() function to take input and calculateTotalScore() function to calculate score of a student from given five marks.So, if the input is like n = 4 scores = [[25, 45, 32, 42, 30], [22, 25, 41, 18, 21], ... Read More

C++ program to hold student info using data hiding and encapsulation

Arnab Chakraborty
Updated on 07-Oct-2021 07:44:04

420 Views

Suppose we want to make student data type with data hiding and encapsulation. Students must have first_name, last_name, age and class items, but these variables cannot be accessible directly. We shall have to define some functions like get_firstname() set_firstname(), get_age() set_age() etc to retrieve and update variable values, and a to_string() function to display students details in this format (age, first_name, last_name, class). From the console take four parameters as input and set them using setter methods that we have defined, and show each items using getter methods and finally using to_string() method.So, if the input is likepriyam kundu 16 ... Read More

C++ program to make student type data and display in proper format

Arnab Chakraborty
Updated on 07-Oct-2021 07:39:53

128 Views

Suppose we have provided the first name, last name, age and class of a student in different lines. We shall have to write a program using structs in C++ to read them all and show in this format (age, first_name, last_name, class). The age and class will be of type integer, and first_name and last_name are of time string.So, if the input is likepriyam kundu 16 10then the output will be (16, priyam, kundu, 10)To solve this, we will follow these steps −define a structure with first_name, last_name of type string and age, cl of type integerread each line and ... Read More

Advertisements