OOPs with C++


 

https://erp-parul.blogspot.com/2026/02/oops-with-c.html
1,4,5,3,2,7,11,9,12,14,15,10,18,13,19,20,22,23,24,25,26,29,30,31,32,33,34,35,36,37
// You are using GCC
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

class HealthProfile
{
    private:
    string name;
    int age;
    double height;
    double weight;
    
    public:
    void setName(const string &n){
        name=n;
    }
    void setAge(int a){
        age=a;
    }
    void setHeight(double h)
    {
        height=h;
    }
    void setWeight(double w){
        weight=w;
    }
    
    string getName(){
        return name;
    }
    int getAge(){
        return age;
    }
    double getHeight(){
        return height;
    }
    double getWeight(){
        return weight;
    }
    double calculateBMI(){
        return weight/(height*height);
    }
    
    void displayProfile(){
        cout<<fixed<<setprecision(2)<<endl;
        cout<<"Name: "<<getName()<<endl;
        cout<<"Age: "<<getAge()<<endl;
        cout<<"Height: "<<getHeight()<<" meters"<<endl;
        cout<<"Weight: "<<getWeight()<<" kg"<<endl;
        cout<<"BMI: "<<calculateBMI()<<endl;
    }
};

int main(){
    HealthProfile p;
    string name;
    getline(cin, name);
    p.setName(name);
    int age;
    cin>>age;
    p.setAge(age);
    double height,weight;
    cin>>height;
    p.setHeight(height);
    cin>>weight;
    p.setWeight(weight);
    
    p.displayProfile();
    return 0;
}

=======================
// You are using GCC
#include<iostream>
#include<string>
using namespace std;

class Employee{
    public:
    string name;
    double salary;
    
    static bool sameSalary(const Employee &emp1, const Employee &emp2){
     return emp1.salary==emp2.salary;   
    }
};

int main(){
    Employee emp1;
    Employee emp2;
    getline(cin,emp1.name);
    cin>>emp1.salary;
    cin.ignore();
    getline(cin,emp2.name);
    cin>>emp2.salary;
    if(Employee::sameSalary(emp1, emp2)){
        cout<<"Both employees have the same salary."<<endl;
    }
    else{
        cout<<"The employees have different salaries."<<endl;
    }
    return 0;
}

=================
Factorial program
// You are using GCC
#include<iostream>
using namespace std;

class Factorial
{
    private: 
    long result;
    
    public:
    Factorial(){
        result=1;
    }
    Factorial calculate(int n){
        Factorial factorial;
        if(n>=0){
            for(int i=1;i<=n;++i){
                factorial.result*=i;
            }
        }
        return factorial;
    }
    long getFactorial(){
        return result;
    }
};
int main()
{
  int n;
  std::cin>>n;
  Factorial factorial = Factorial().calculate(n);
  if(n<0){
      std::cout<<"Invalid input"<<std::endl;
  }
  else{
      std::cout<<"Factorial of "<<n <<" is: "<<factorial.getFactorial()<<std::endl;
  }
    return 0;
}

Comments

Popular posts from this blog

Software Engineering Documents

Software Engineering PPT