Write a C++ program to declare structure.
Initialize and display contents of member
variables
#include <iostream>
#include <string>
using namespace std;
// Declaring the structure
struct Student {
string name;
int age;
float grade;
};
int main() {
// Initializing a structure variable
Student student1 = {"John Doe", 20, 9.5};
// Displaying the contents of the structure
cout << "Student Name: " << [Link] << endl;
cout << "Student Age: " << [Link] << endl;
cout << "Student Grade: " << [Link] << endl;
return 0;
Student Name: John Doe
Student Age: 20
Student Grade: 9.5
Write a C++ program to display Names, Roll No., and grade of N students who have
appeared in the examination. Declare the class of name, roll no., and grade. Create an
array of class objects. Read and display the contents of the array.
#include <iostream>
using namespace std;
class Student
{
public:
string name;
int rollno;
char grade;
// constructor to create object
Student (string n, int r, char c)
{
name =n;
rollno =r;
grade= c;
}
// display method
void display()
{
cout<<"the name is: "<<name<<endl;
cout<<" the roll no is :"<<rollno<<endl;
cout<<"the grade is :"<<grade<<endl;
}
};
int main()
{
Student Stu[3] = { Student("HoshanReddy", 150, 'O'),
Student("ThoshanReddy", 151, 'A'),
Student("sumanthReddy", 152, 'b')};
for (int i=0;i<3;i++)
{
Stu[i].display();
}
return 0;
}
the name is: HoshanReddy
the roll no is :150
the grade is :O
the name is: ThoshanReddy
the roll no is :151
the grade is :A
the name is: sumanthReddy
the roll no is :152
the grade is :b
Write a C++ Program to find SQUARE and CUBE of a number N using inline functions.
#include <iostream>
using namespace std;
// Inline function to calculate square
inline int square(int n)
return n * n;
// Inline function to calculate cube
inline int cube(int n)
return n * n * n;
int main()
int N;
cout << "Enter a number: ";
cin >> N;
cout << "Square of " << N << " is: " << square(N) << endl;
cout << "Cube of " << N << " is: " << cube(N) << endl;
return 0;
Enter a number: 2
Square of 2 is: 4
Cube of 2 is: 8
Using encapsulation, write a program to compute AREA and PERIMETER of a rectangle.
#include<iostream>
using namespace std;
class Rectangle
private:
int length;
int breadth ;
public:
void setdimensions(float l, float b)
if(l>0 && b>0)
length = l;
breadth =b;
else
cout<< " enter valid data "<<endl;
float getarea()
return length* breadth;
float getperimeter()
{
return 2*( length+breadth);
};
int main()
Rectangle r;
[Link](2,3);
cout<<[Link]()<<" "<< [Link]();
return 0;
Output:
6 10
Write a program to add two COMPLEX Numbers by overloading ‘+’ operator.
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor
Complex(float r = 0.0, float i = 0.0) {
real = r;
imag = i;
// Overloading the '+' operator
Complex operator + (const Complex& obj) {
Complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return temp;
// Function to display complex number
void display() {
cout << real << " + " << imag << "i" << endl;
};
int main() {
Complex c1(3.5, 2.5);
Complex c2(1.5, 4.5);
Complex result = c1 + c2;
cout << "Sum of complex numbers: ";
[Link]();
return 0;
Output:
Sum of complex numbers: 5 + 7i
Write a program to show inheritance taking the base class as Animal.
#include<iostream>
using namespace std;
class Animal
{
public:
void makesound()
{
cout<<"animal makes sound"<<endl;
}
void makesound(string mood)
{
cout<<"animal makes :"<<mood<<"sound"<<endl;
}
virtual void shout()
{
cout<< "animal shouts "<<endl;
}
};
class dog : public Animal
{
void shout() override
{
cout<<"dog makes Bow ! Bow ! sound"<<endl;
}
};
class cat : public Animal
{
void shout() override
{
cout<<"cat makes Meow ! Meow ! sound"<<endl;
}
};
int main()
{
Animal a;
cout<<" Function overloading"<<endl;
[Link]();
[Link]("Happy");
cout<<"demonstrating function overriding"<<endl;
Animal* ptr;
dog d;
cat c;
ptr= &d;
ptr->shout();
ptr = &c;
ptr->shout();
return 0;
Output:
Function overloading
animal makes sound
animal makes :Happysound
demonstrating function overriding
dog makes Bow ! Bow ! sound
cat makes Meow ! Meow ! sound