0% found this document useful (0 votes)
12 views

c++docfile (1)

The document provides a series of C++ programming tasks, each accompanied by example code. These tasks cover fundamental concepts such as printing output, using operators, control statements, classes, constructors, access specifiers, function and operator overloading, static functions, inheritance, and friend functions. Each example demonstrates the implementation of the respective concept in C++.

Uploaded by

priya.rajak
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)
12 views

c++docfile (1)

The document provides a series of C++ programming tasks, each accompanied by example code. These tasks cover fundamental concepts such as printing output, using operators, control statements, classes, constructors, access specifiers, function and operator overloading, static functions, inheritance, and friend functions. Each example demonstrates the implementation of the respective concept in C++.

Uploaded by

priya.rajak
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/ 17

1) Write a program to Print Hello world in c++

Input:
#include <iostream>

int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Output:

2) Write a program to demonstrate operators in c++

Input:
#include <iostream>
using namespace std;
int main() {
int num1 = 15, num2 = 4;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
cout << "Addition: " << num1 + num2 << endl;
cout << "Subtraction: " << num1 - num2 << endl;
cout << "Multiplication: " << num1 * num2 << endl;
cout << "Division (integer result): " << num1 / num2 << endl;
cout << "Modulus (remainder): " << num1 % num2 << endl;
cout << "Division (floating point result): " << (float)num1 /
num2 << endl;
return 0;
}
Output:

3) Write a c++ program to use condition statements and for loops.

Input:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
cout << "Number: " << i << " - ";
if (i % 2 == 0) {
cout << "Even" << endl;
} else {
cout << "Odd" << endl;
}
}
return 0;
}

Output:
4) Write a program to demonstrate switch loop.

Input:
#include <iostream>
using namespace std;
int main() {
char oper;
float num1,num2;
cout<<"Enter an operator(+,-,*,/):";
cin>>oper;
cout<<"Enter two numbers:"<<endl;
cin>>num1>>num2;
switch(oper){
case'+':
cout<<num1<<"+"<<num2<<"="<<num1+num2;
break;
case'-':
cout<<num1<<"-"<<num2<<"="<<num1-num2;
break;
case'*':
cout<<num1<<"*"<<num2<<"="<<num1*num2;
break;
case'/':
cout<<num1<<"/"<<num2<<"="<<num1/num2;
break;
default:
cout<<"Error! The operator is not correct";
break;
}
return 0;
}

Output:

5) Write a Program to create array in c++ with class and objects.

Input:
#include<iostream>
using namespace std;

class Employee
{
int id;

char name[30];
public:
void getdata();
void putdata();
};
void Employee::getdata(){
cout<<"Enter Id:";

cin>>id;
cout<<"Enter Name:";
cin>>name;
}

void Employee::putdata(){
cout<<id<<" ";
cout<<name<<" ";
cout<<endl;
}

int main()
{
Employee emp;
emp.getdata();
emp.putdata();
return 0;

Output:

6) Write a program to create a class with multiple objects.

Input:
#include<iostream>
#include<string>
using namespace std;

class MyClass{
public:
int myNum;
string myString;
};

int main(){
MyClass myObj;

myObj.myNum=15;
myObj.myString="Some text";

cout<<myObj.myNum<<"\n";
cout<<myObj.myString;
return 0;

Output:

7) Write a program to use constructors, parameter constructor.

Input:(Constructor)
#include<iostream>
using namespace std;
class MyClass{
public:
MyClass(){
cout<<"Hello World!";
}
};

int main(){
MyClass myObj;
return 0;
}

Output:

Input:(Parameter constructor)
#include<iostream>
using namespace std;

class Car{
public:
string brand;
string model;
int year;
Car(string x,string y,int z){
brand=x;
model=y;
year=z;
}
};

int main(){
Car carObj1("BMW","X5",1999);
Car carObj2("FORD","MUSTANG",1969);

cout<<carObj1.brand<<" "<<carObj1.model<<"
"<<carObj1.year<<"\n";
cout<<carObj2.brand<<" "<<carObj2.model<<"
"<<carObj2.year<<"\n";
return 0;

Output:

8) Write a c++ program to show the use of access specifier.

Input:
#include<iostream>
using namespace std;

class Employee{
private:
int salary;

public:
void setSalary(int s){
salary=s;
}
int getSalary(){
return salary;
}
};

int main(){
Employee myObj;
myObj.setSalary(50000);
cout<<myObj.getSalary();
return 0;
}

Output:

9) Write a program for constructor overloading.

Input:
#include <iostream>
using namespace std;

class construct{
public:
float area;
construct()
{
area=0;
}
construct(int a, int b)
{
area=a*b;
}
void disp()
{
cout<<area<<endl;
}
};

int main()
{
construct o;
construct o2(10,20);

o.disp();
o2.disp();
return 1;

Output:

10) Write a program for function overloading .

Input:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}

int main() {
int intResult = add(5, 3);
float floatResult = add(5.5f, 3.2f);
int intTripleResult = add(2, 3, 4);

cout << "Addition of two integers: " << intResult << endl;
cout << "Addition of two floating point numbers: " <<
floatResult << endl;
cout << "Addition of three integers: " << intTripleResult <<
endl;

return 0;
}

Output:

11) Write a program for operator overloading.


Input:
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag;
public:
Complex(float r = 0.0, float i = 0.0) {
real = r;
imag = i;
}
Complex operator+(const Complex& other) {
return Complex(real + other.real, imag + other.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3.5, 2.5), c2(1.5, 4.5);
Complex c3 = c1 + c2;
cout << "Sum of the complex numbers: ";
c3.display();
return 0;
}

Output:

12) Write a program to define static function.


Input:
#include <iostream>
using namespace std;
class MyClass {
private:
static int count;
public:
static void incrementCount() {
count++;
}
static void displayCount() {
cout << "Count: " << count << endl;
}
};
int MyClass::count = 0;

int main() {
MyClass::incrementCount();
MyClass::incrementCount();
MyClass::displayCount();
return 0;
}

Output:

13) Write a c++ program to show inheritance.

Input:(Hybrid inheritance)
#include<iostream>
using namespace std;
class A
{
public:
int x;
};
class B : public A
{
public:
B()
{
x = 10;
}
};
class C
{
public:
int y;
C()
{
y = 4;
}
};
class D : public B, public C
{
public:
void sum()
{
cout << "Sum="<<x+y;
}
};
int main()
{
D obj1;
obj1.sum();
return 0;
}

Output:

(Multilevel inheritance):
#include<iostream>
using namespace std;
class MyClass {
public:
void myFunction(){
cout << "Some content in parent class.";
}
};

class MyChild: public MyClass{


};

class MyGrandChild: public MyChild {


};

int main(){
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
Output:

14) Write a program to use friend function in class.

Input:
#include<iostream>
using namespace std;

class GFG{
private:
int private_variable;

protected:
int protected_variable;

public:
GFG()
{
private_variable = 10;
protected_variable = 99;
}

friend class F;
};

class F {
public:
void display(GFG& t)
{
cout << "The value of Private Variable = " <<
t.private_variable << endl;
cout << "The value of Protected Variable = " <<
t.protected_variable;
}
};

int main()
{
GFG g;
F fri;
fri.display(g);
return 0;
}

Output:

You might also like