class in c++
- #include<iostream>
- #include<string.h>
- using namespace std;
- class shafiq
- {
- bool con;
- char a;
- int b;
- float c;
- double d;
- long e;
- public:
- shafiq();
- ~shafiq();
- void get();
- void show();
- private:
- }
- ;
- shafiq::shafiq():con(0),a('0'),b(0),c(0),d(0),e(0)
- {
- }
- shafiq::~shafiq()
- {
- }
- void choise(shafiq std)
- {
- int ch;
- do
- {
- cout << "You are in choise function " << endl ;
- cout << "Plz enter your choise " << endl ;
- cin>>ch;
- switch(ch)
- {
- case 1:
- std.get();
- break;
- case 2:
- std.show();
- break;
- default:
- cout << "Press any key to continue . . ." << endl ;
- }
- }while(ch!=0);
- }
- void shafiq::get()
- {
- cin>>con;
- cin>>a;
- cin>>b;
- cin>>c;
- cin>>d;
- cin>>e;
- }
- void shafiq::show()
- {
- cout << "BOOL " <<con << endl ;
- cout << "char " <<a << endl ;
- cout << "int" << b << endl ;
- cout << "float " << c << endl ;
- cout << "double" << d << endl ;
- cout << "long" << e << endl ;
- }
- void main()
- {
- shafiq std;
- choise(std);
- system("pause");
- }
Class Constructor and Destructor
- #include<iostream>
- using namespace std;
- int flag=0;//global variable
- class student
- {
- public:
- student();//default constructor
- student(int tid,int tmarks);//argumeted constructor
- ~student();//destructor
- void showid()//function for show id
- {
- cout << id << endl;
- }
- void showmarks()//function for showing marks
- {
- //by default this function if inline function
- cout << marks << endl ;
- }
- void show_data();
- {
- cout <<" id is " << id << endl ;
- cout <<" marks is " << marks << endl ;
- }
- private:
- int id;
- int marks;
- };
- student::student()//default constructor defination
- {
- cout << "you are in constructor " << endl ;
- id=0;
- marks=0;
- flag++;
- }
- student ::student(int tid, int tmarks)//argumented constructor defination
- {
- cout << "you are in argumented constructor " << endl ;
- id=tid;
- marks=tmarks;
- flag++;
- }
- student::~student()//defination of destructor
- {
- cout << "you are in destructor " << endl ;
- flag++;
- }
- void main()
- {
- int flag=100;
- student st;
- student std(23,2324);
- std.showid();
- std.showmarks();
- std.~student();
- cout << "value of flag " << ::flag <<endl;
- system("pause");
- }
- // =======================================================
- // This program adds two numbers and then averages them.
- // =======================================================
- #include <iostream>
- using namespace std;
- int main ()
- {
- double num1 = 0;
- double num2 = 0;
- double total = 0;
- double avg = 0;
- cout << "Please enter a number: ";
- cin >> num1;
- cout << "Please enter a second number: ";
- cin >> num2;
- total = num1 + num2;
- cout << "\nThe total of the two numbers is: " << total << "\n";
- avg = (total / 2);
- cout << "The average of the two numbers is: " << avg << "\n\n";
- return 0;
- }
- /* ===================== output ==================================
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
- #include<iostream>
- using namespace std;
- void main()
- {
- int length=10;
- for (int i = 0; i <= length; i++)
- {
- for (int j = 0; j <= length; j++)
- {
- if(i==j ||j==0 ||j==length)
- cout<<"*";
- else
- cout<<" ";
- }
- cout<<endl;
- }