Tuesday, 3 December 2019

Sample Programs

# include<iostream>
using namespace std;

class Test
{
private:
int num1;
static const int num2;
public:
Test(void)
{
this->num1=10;
}
void PrintRecord(void)
{
cout<<"Num1 : "<<this->num1<<endl;
cout<<"Num2 : "<<this->num2<<endl;
}
};
const int Test::num2=1000;
int main(void)
{
Test t1;
t1.PrintRecord();
return 0;
}




# include<iostream>
using namespace std;

class A
{
public:
static void F1(void)
{
cout<<"A::F1"<<endl;
}
};

class B
{
public:
static void F2(void)
{
cout<<"B::F2"<<endl;
}
static void F3(void)
{
A::F1();
F2();
B::F2();
cout<<"B::F3"<<endl;
}
};

int main(void)
{
B::F3();
return 0;
}



# include<iostream>
using namespace std;
#include <string>

namespace NEmployee
{
class TEmployee
{
private:
string _name;
int _empid;
float _salary;
static int _count;
public:
TEmployee(void)
{
this->_name=" ";
this->_empid=++TEmployee::_count;
this->_salary=0;
}
TEmployee(const string name,const float salary)
{
this->_name=name;
this->_empid= ++TEmployee::_count;
this->_salary=salary;
}
static void SetCount(int count)
{
TEmployee::_count=count;
}
void PrintRecord()
{
cout << this->_name<<" "<<this->_empid<<" "<<this->_salary << "\n";
}
};
int TEmployee::_count;
int main(void)
{
using namespace NEmployee;
TEmployee::SetCount(1000);
TEmployee emp1("ABC",10000.00f);
emp1.PrintRecord();
return 0;
}




# include<iostream>
using namespace std;
#include<string>

class TPerson
{
private:
string _name;
int _age;

public:
TPerson(void)
{
this->_name="";
this->_age=0;
}
TPerson(const string name,const int age):_name(name),_age(age)
{}
void PrintRecord(void)const
{
cout<<"Name: "<<this->_name<<endl;
cout<<"Age: "<<this->_age<<endl;
}
};
class TEmployee:public TPerson
{
private:
int _empid;
float _salary;
public:
TEmployee(void):_empid(0),_salary(0)
{}
TEmployee(const string name,const int age,const int empid,const int salary):TPerson(name,age),_empid(empid),_salary(salary)
{}
void PrintRecord(void)const
{
TPerson::PrintRecord();
cout<<"EmpId: "<<this->_empid<<endl;
cout<<"Salary: "<<this->_salary<<endl;
}

};
int main(void)
{
TEmployee emp("ABC",23,123,1200);
emp.PrintRecord();
return 0;
}


No comments:

Post a Comment