// String
#include <iostream>
using namespace std;
class str{
string s;
public:
void set(string x){
s=x;
}
void display(){
cout<<s<<endl;
}
void operator+(str &A){
s.append(A.s);
}
void operator=(str &A){
s=A.s;
}
bool operator<=(str &A){
if(s.length()<A.s.length()){
return true;
}
return false;
}
void len(){
cout<<s.length()<<endl;
}
void to_up(){
for(int i=0;i<s.length();i++){
if(s[i]>='a' && s[i]<='z'){
s[i]=s[i]-32;
}
}
}
void to_low(){
for(int i=0;i<s.length();i++){
if(s[i]>='A'&&s[i]<='Z'){
s[i]=s[i]+32;
}
}
}
};
int main() {
str a,b,c,d;
a.set("Helllo");
b.set("Programmer");
a+b;
a.display();
a.len();
c.set("Welcome");
d=c;
d.display();
cout<<(d<=a)<<endl;
d.to_up();
d.display();
a.to_low();
a.display();
return 0;
}
// matrix multiplication
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
// cylinder wala
#include<iostream>
using namespace std;
class Cylinder{
float r,h;
float Pi=3.14;
public:
float area();
float vol();
float s_area();
float read(float,float);
float show();
};
float Cylinder::area(){
return 2*Pi*r*(r+h);
}
float Cylinder::vol(){
return 2*Pi*r*r*h;
}
float Cylinder::s_area(){
return 2*Pi*r*h;
}
float Cylinder::read(float r_in, float h_in){
r=r_in;
h=h_in;
}
void Cylinder::show(){
cout<<"area"<<area();
cout<<"vol"<<vol();
cout<<"surface area"<<s_area();
}
int main(){
Cylinder c;
float r,h;
cout<<"r"<<endl;
cin>>r;
cout<<"h"<<endl;
cin>>h;
c.read(r,h);
c.show();
return 0;
}
//maximum number using friend function
#include<iostream>
using namespace std;
class number2;
class number1
{
float a;
public:
void getdata1()
{
cout<<"Enter value for first number:";
cin>>a;
}
friend float max(number1,number2);
};
class number2
{
float b;
public:
void getdata2()
{
cout<<"Enter value for second number:";
cin>>b;
}
friend float max(number1,number2);
};
float max(number1 A, number2 B)
{
if(A.a>B.b)
return A.a;
else return B.b;
}
int main()
{
number1 d;
number2 e;
d.getdata1();
e.getdata2();
cout<<"Greater of two numbers is: "<<max(d,e);
return 0;
}
//Addition of two complex number using constructor overloading
#include<iostream>
using namespace std;
class complex
{
float real, imag;
public:
complex() { }
complex(float a) {real=imag=a;}
complex(float x, float y) {real=x; imag=y;}
void sum(complex c1, complex c2){
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
void show()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
};
int main(){
float a, b, c ;
cout<<"Enter the values of a and b and c:"<<endl;
cin>>a>>b>>c;
complex c1(a);
complex c2(b,c);
c2.sum(c1,c2);
c2.show();
return 0;
}
//multilevel inheritence
#include<iostream>
using namespace std;
class Student{
protected:
int roll;
public:
void getroll(int x){
cout<<"enter roll"<<endl;
cin>>x;
roll=x;
}
};
class Test: public Student{
protected:
float sub1,sub2,sub3;
public:
void getmarks(float a, float b, float c){
cout<<"enter sub1"<<endl
cin>>a;
cout<<"enter sub2"<<endl
cin>>b;
cout<<"enter sub3"<<endl
cin>>c;
sub1=a;
sub2=b;
sub3=c;
}
void putmarks(){
cout<<"marks are"<<sub1<<" "<<sub2<<" "<<sub3<<endl;
}
};
class Result: public Test{
float total;
public:
void setans(){
total=sub1+sub2+sub3;
}
void show(){
cout<<"totsl"<<total;
}
};
int main(){
Result R1;
R1.getroll(10);
R1.getmarks(100,100,100);
R1.setans();
R1.putmarks();
R1.showtotal();
retrun 0;
}
// multiply using class template
#include<iostream>
using namespace std;
template <class T>
class Calculate{
private:
T a;
T b;
public:
Calculate(T a, T b);
T add();
T mul();
};
template<class T>
Calculate <T>::Calculate(T x, T y){
a=x;
b=y;
}
template<class T>
T Calculate<T>::add(){
T c;
c=a+b;
return c;
}
template<class T>
T Calculate<T>::mul(){
T c;
c=a*b;
return c;
}
int main(){
Calculate<int> obj(10,5);
cout<<obj.add()<<endl;
cout<<obj.mul()<<endl;
}
// virtual
#include <iostream>
using namespace std;
class base{
public :
virtual void display(){
cout << "base class" << endl;
}
};
class derived : public base
{
public:
void display()
{
cout << "derived class" << endl;
}
};
int main()
{
derived d;
base *b = &d;
b -> display();
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}