C++ programming

class in c++





  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. class shafiq
  5. {
  6. bool con;
  7. char a;
  8. int b;
  9. float c;
  10. double d;
  11. long e;

  12. public:
  13. shafiq();
  14. ~shafiq();
  15. void get();
  16. void show();
  17. private:

  18. }
  19. ;

  20. shafiq::shafiq():con(0),a('0'),b(0),c(0),d(0),e(0)
  21. {
  22. }

  23. shafiq::~shafiq()
  24. {
  25. }
  26. void choise(shafiq std)
  27. {
  28. int ch;
  29. do
  30. {
  31. cout << "You are in choise function " << endl ;
  32. cout << "Plz enter your choise " << endl ;
  33. cin>>ch;
  34. switch(ch)
  35. {
  36. case 1:
  37. std.get();
  38. break;

  39. case 2:
  40. std.show();
  41. break;


  42. default:
  43. cout << "Press any key to continue . . ." << endl ;
  44. }
  45. }while(ch!=0);
  46. }
  47. void shafiq::get()
  48. {
  49. cin>>con;
  50. cin>>a;
  51. cin>>b;
  52. cin>>c;
  53. cin>>d;
  54. cin>>e;
  55. }
  56. void shafiq::show()
  57. {
  58. cout << "BOOL " <<con << endl ;
  59. cout << "char  " <<a << endl ;
  60. cout << "int" << b << endl ;
  61. cout << "float " << c << endl ;
  62.     cout << "double" << d << endl ;
  63. cout << "long" << e << endl ;
  64. }


  65. void main()
  66. {
  67. shafiq std;

  68.  choise(std);

  69. system("pause");
  70. }

Class Constructor and Destructor


  1. #include<iostream>
  2. using namespace std;
  3.  int flag=0;//global variable 
  4. class student
  5. {
  6. public:
  7. student();//default constructor
  8. student(int tid,int tmarks);//argumeted constructor 
  9. ~student();//destructor
  10.     void  showid()//function for show id 
  11. {
  12. cout << id << endl;
  13. }
  14. void  showmarks()//function for showing marks
  15. {
  16. //by default this function if inline function
  17. cout <<  marks << endl ;
  18. }
  19. void show_data();
  20. {
  21. cout <<" id is " << id << endl ;
  22. cout <<" marks is " << marks << endl ;
  23. }
  24. private:

  25. int id;
  26. int marks;
  27.  
  28. };

  29. student::student()//default constructor defination 
  30. {
  31. cout << "you are in constructor " << endl ;
  32. id=0;
  33. marks=0;
  34. flag++;

  35. }

  36. student ::student(int tid, int tmarks)//argumented constructor defination
  37. {
  38. cout << "you are in argumented constructor " << endl ;
  39. id=tid;
  40. marks=tmarks;
  41. flag++;
  42. }
  43. student::~student()//defination of destructor 
  44. {
  45. cout << "you are in destructor " << endl ;
  46. flag++;
  47. }

  48. void main()
  49. {
  50. int flag=100;
  51. student st;
  52. student std(23,2324);
  53. std.showid();
  54. std.showmarks();
  55. std.~student();
  56. cout << "value of flag " << ::flag <<endl;

  57. system("pause");
  58.  
  59. }




  1. // =======================================================
  2. // This program adds two numbers and then averages them.
  3. // =======================================================
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9.        double num1 = 0;
  10.        double num2 = 0;
  11.        double total = 0;
  12.        double avg = 0;

  13.        cout << "Please enter a number: ";
  14.        cin >> num1;
  15.        cout << "Please enter a second number: ";
  16.        cin >> num2;
  17.  
  18.        total = num1 + num2;
  19.        cout << "\nThe total of the two numbers is: " << total << "\n";
  20.  
  21.        avg = (total / 2);
  22.  
  23.        cout << "The average of the two numbers is: " << avg << "\n\n";
  24.  
  25.        return 0;
  26. }
  27. /* ===================== output ==================================
Please enter a number: 88.6
Please enter a second number: 75.3

The total of the two numbers is: 163.9
The average of the two numbers is: 81.95

Press any key to continue . . .
*/// =============================================================


C++ Source Code: Count even numbers using a: for loop C++
// - Count even numbers using a for loop
#include <iostream>
using namespace std;
int main()
{
for (int count = 0; count <= 26; count += 2)
{
cout << count << ", ";
}
cout << endl;
return 0;
}

/*==================[output]======================
0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26,
Press any key to continue . . .
=================================================*/
// end of program


To Print N on screen



  1. #include<iostream>
  2. using namespace std;

  3. void main()
  4. {
  5. int length=10;
  6. for (int i = 0; i <= length; i++)
  7. {
  8. for (int j = 0; j <= length; j++)
  9. {
  10. if(i==j ||j==0 ||j==length)
  11. cout<<"*";
  12. else
  13. cout<<" ";
  14. }
  15. cout<<endl;

  16. }
  17.  
Out put :