Skip to main content

C++



1.sum


/* The extension name of C++ is .cpp*/
/*Sum of two numbers*/
#include<iostream>
#include<conio.h>
using namespace std;
 int main()
{
    int a,b,c;
cout << " \nenter your 1st  no;   ";
    cin>>a;
     cout<<"\nenter your 2nd  no;  ";
    cin>>b;
    c=a+b;
     cout<<"\nsum of those no is;   "<<c;
   return 0;
}

O/P:

enter your 1st  no;   23

enter your 2nd  no;  34

sum of those no is;   57

2.voter non voter

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout<<"\nEnter your age:   ";
cin>>a;
if(a>=18)
{
cout<<"\nyou are voter";
}
else
{
cout<<"\nyou are non voter";
}
return 0;
}


O/P:
Enter your age:   45

you are voter

3.odd even

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout<<"\nEnter number:   ";
cin>>a;
if(a%2==0)
{
cout<<"\nthe number is odd";
}
else
{
cout<<"\nthe number is even";
}
return 0;
}

O/P:

Enter number:   34

the number is odd

4.leap year or not

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
cout<<"\nEnter a year:   ";
cin>>a;
if(a%4==0)

if(a%100==0)

if(a%400==0)
{
cout<<"\nThe year is leap year";
}
else
{
cout<<"\nThe year is not leap year";
}
else
{
cout<<"\nThe year is  leap year";
}
else
{
cout<<"\nThe year is not leap year";
}
return 0;
}

O/P:

Enter a year:   1900

The year is not leap year

5

/*maximum of two numbers*/
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
cout<<"\nEnter 1st number:   ";
cin>>a;
cout<<"\nEnter 2nd number:   ";
cin>>b;
if(a>b)
{
cout<<"\nmaximum number is:    "<<a;
}
else 
{
cout<<"\nmaximum number is:   "<<b;
}
return 0;
}


O/P:

Enter 1st number:   45

Enter 2nd number:   34

maximum number is:    45

6.0 class


class <space> class name
{
private:
variable;
public:
function
};

Q. How to create object?
A. void main()
{
class <space> class name <space> object name;

6.1

/*sum of two  numbers using class*/
#include <iostream>
#include<conio.h>
using namespace std;
class num
{
private:
    int a,b,c;
   
public:
    void getdata()
    {
      cout<<"\nEnter 1st number:  ";
      cin>>a;
      cout<<"\nEnter 2nd number:   ";
      cin>>b;
     
    }
    void sum()
    {
        c=a+b;
    }
  
    void showdata()
    {
      cout<<"\nthe sum is:   "<<c;

    }
};
int main()
{
    class num n;
    n.getdata();
    n.sum();
   
    n.showdata();
   return 0;
}

O/P:

Enter 1st number:  24

Enter 2nd number:   324

the sum is:   348

6.2

/*square value using class*/
#include <iostream>
#include<conio.h>
using namespace std;
class num
{
private:
    int a,b;
    
public:
    void getdata()
    {
      cout<<"\nEnter a number:  ";
      cin>>a;
      
    }
    void sqr()
    {
        b=a*a;
    }
   
    void showdata()
    {
      cout<<"\nthe square is:   "<<b;
    
    }
};
int main()
{
    class num p;
    p.getdata();
    p.sqr();
   
    p.showdata();
   return 0;
}

O/P:

Enter a number:  23

the square is:   529

6.3 student

#include <iostream>
#include<conio.h>
using namespace std;
class student
{
private:
    int roll;
    char name[30];
    int sub1;
    int sub2;
    int sub3;
    int t;
    float avg;
public:
    void getdata()
    {
      cout<<"\nEnter roll:";
      cin>>roll;
      cout<<"\nEnter name:";
      cin>>name;
      cout<<"\nEnter sub1:";
      cin>>sub1;
      cout<<"\nEnter sub2:";
      cin>>sub2;
      cout<<"\nEnter sub3:";
      cin>>sub3;
    }
    void total()
    {
        t=sub1+sub2+sub3;
    }
    void average()
    {
        avg=(float)t/3;
    }
    void showdata()
    {
      cout<<"\nthe roll no is:"<<roll;
     cout<<"\nthe name is:"<<name;
     cout<<"\nthe sub1 is:"<<sub1;
     cout<<"\nthe sub2 no is:"<<sub2;
     cout<<"\nthe sub3 is:"<<sub3;
     cout<<"\nthe total is:"<<t;
     cout<<"\nthe avg is:"<<avg;
    }
};
int main()
{
    class student s;
    s.getdata();
    s.total();
    s.average();
    s.showdata();
    getch();
}

O/P:

Enter roll:1

Enter name:ajay

Enter sub1:67

Enter sub2:89

Enter sub3:48

the roll no is:1
the name is:ajay
the sub1 is:67
the sub2 no is:89
the sub3 is:48
the total is:204
the avg is:68

6.4 students

#include <iostream>
#include<conio.h>
using namespace std;
class student
{
private:
    int roll;
    char name[30];
    int sub1;
    int sub2;
    int sub3;
    int t;
    float avg;
public:
    void getdata()
    {
      cout<<"\nEnter roll:";
      cin>>roll;
      cout<<"\nEnter name:";
      cin>>name;
      cout<<"\nEnter sub1:";
      cin>>sub1;
      cout<<"\nEnter sub2:";
      cin>>sub2;
      cout<<"\nEnter sub3:";
      cin>>sub3;
    }
    void total()
    {
        t=sub1+sub2+sub3;
    }
    void average()
    {
        avg=(float)t/3;
    }
    void showdata()
    {
      cout<<"\nthe roll no is:"<<roll;
     cout<<"\nthe name is:"<<name;
     cout<<"\nthe sub1 is:"<<sub1;
     cout<<"\nthe sub2 no is:"<<sub2;
     cout<<"\nthe sub3 is:"<<sub3;
     cout<<"\nthe total is:"<<t;
     cout<<"\nthe avg is:"<<avg;
    }
};
int main()
{
    class student stu[500];
    int n,i;
    cout<<"\nenter total students:";
    cin>>n;
    i=0;
    while(i<n)
    {
        cout<<"\nenter your "<<i+1<<"th student details:\n";
        stu[i].getdata();
        i++;
    }
    i=0;
    while(i<n)
    {

    stu[i].total();
    stu[i].average();
    stu[i].showdata();
    cout<<"\n ";
    i++;
    }
    getch();
}

O/P:

enter total students:5

enter your 1th student details:

Enter roll:1

Enter name:ritu

Enter sub1:90

Enter sub2:98

Enter sub3:78

enter your 2th student details:

Enter roll:8

Enter name:sunil

Enter sub1:76

Enter sub2:78

Enter sub3:89

enter your 3th student details:

Enter roll:4

Enter name:vinay

Enter sub1:76

Enter sub2:93

Enter sub3:75

enter your 4th student details:

Enter roll:7

Enter name:raju

Enter sub1:68

Enter sub2:44

Enter sub3:67

enter your 5th student details:

Enter roll:6

Enter name:joti

Enter sub1:65

Enter sub2:43

Enter sub3:65

the roll no is:1
the name is:ritu
the sub1 is:90
the sub2 no is:98
the sub3 is:78
the total is:266
the avg is:88.6667

the roll no is:8
the name is:sunil
the sub1 is:76
the sub2 no is:78
the sub3 is:89
the total is:243
the avg is:81

the roll no is:4
the name is:vinay
the sub1 is:76
the sub2 no is:93
the sub3 is:75
the total is:244
the avg is:81.3333

the roll no is:7
the name is:raju
the sub1 is:68
the sub2 no is:44
the sub3 is:67
the total is:179
the avg is:59.6667

the roll no is:6
the name is:joti
the sub1 is:65
the sub2 no is:43
the sub3 is:65
the total is:173
the avg is:57.6667

7.scope resolution

/*sum of two numbers*/
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
    int a,b,t;
public:
    int getdata();
    int total();
    int showdata();
};
int student :: getdata()
{
    cout<<"\nEnter your 1st no: ";
    cin>>a;
    cout<<"\nEnter your 2nd no: ";
    cin>>b;
}
int student :: total()
{
   t=a+b;
}
int student :: showdata()
{
    cout<<"\nThe total is: "<<t;
}
int main()
{
    class student s;
    s.getdata();
    s.total();
    s.showdata();
    return 0;
}

O/P:

Enter your 1st no: 234

Enter your 2nd no: 324

The total is: 558

8.constructor


#include<iostream>
#include<conio.h>
using namespace std;
class number
{
private:
    int a,b,c;
public:
    number()
    {
        a=5;
        b=6;
    }
    int total()
    {
        c=a+b;

    }
    int showdata()
    {
        cout<<"\nThe total is: "<<c;

    }

};
int main()
{
    class number n;
n.total();
n.showdata();
return 0;
}


O/P:
The total is: 11

9.destructor

#include <iostream>
using namespace std;
class HelloWorld
{
public:
  //Constructor
  HelloWorld()
{
    cout<<"Constructor is called"<<endl;
  }
  //Destructor
  ~HelloWorld()
{
    cout<<"Destructor is called"<<endl;
   }
   //Member function
   void display()
{
     cout<<"Hello World!"<<endl;
   }
};
int main()
{
   //Object created
   HelloWorld obj;
   //Member function called
   obj.display();
   return 0;
}


O/P:
Constructor is called
Hello World!
Destructor is called

Comments