c++docfile (1)
c++docfile (1)
Input:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
Output:
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:
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:
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:
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:
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:
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:
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:
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:
Output:
int main() {
MyClass::incrementCount();
MyClass::incrementCount();
MyClass::displayCount();
return 0;
}
Output:
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.";
}
};
int main(){
MyGrandChild myObj;
myObj.myFunction();
return 0;
}
Output:
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: