I am try to write a C++ program that tells the eldest and youngest sibling in a family. I am write this program declaring a class.I write all code program but my program does not compiles and give oprator error. Please someone check this and make it correct.I need this type of output
Please enter the particulars of each sibling:
Please enter the particulars of Sibling: 1
Name: Arif Age: 20
Please enter the particulars of Sibling: 2
Name: Sana Age: 16
Please enter the particulars of Sibling: 3
Name: Sobia Age: 13
Please enter the particulars of Sibling: 4
Name: Amna Age: 10
My rogram source code is here

#include <iostream.h>
#include <string.h>
#include <conio.h>

// defining the Sibling class
class Sibling
{
    // hidden part of the class
    private:
        char *name;    // for Sibling name
        int age;       // for Sibling age
     // interface of the class
     public:
        Sibling(); //constructor
        Sibling(const Sibling &obj);//copy constructor
        void setname(char *);//setter function for name
        void setage(int a);  //setter function for age
        char *getname(); //getter function for name
        int getage();    //getter function for age  
        ~Sibling();         //destructor
        bool operator >(int *array int arraysize);                        // overloading assignment operator
};
bool Sibling::operator >( int *array, int arraysize)
{
    for(int i=0;i<arraysize;i++)
    if a[i]>a[i+1];
    {
    int *temp=a[i];
    a[i]=a[i+1];
    a[i+1]=*temp;
    }
}
Sibling::~Sibling()//destructor
{
    if (name)
    {
    delete[]name;
    }
}

Sibling::Sibling(const Sibling &obj)
{
    int len=strlen(obj.name);
    name=new char(len+1);
    strcpy(name,obj.name);
    age=obj.age;
}
// setting the Sibling name
void Sibling::setname(char *SiblingName)
{
    name= new char(strlen(SiblingName+1));
    strcpy(name,SiblingName);
}
// setting the Sibling age
void Sibling::setage(int SiblingAge)
{
    if (age<=0)
    {
    cout<<"Negative age is not allowed";
    age=0;
    }
    age=SiblingAge;
}
// getting the Sibling name
char *Sibling ::getname()
{
    return name;
}
// getting the Sibling age
int Sibling:: getage()
{
    return age;
}

//Main program. We will take  Sibling object  

int main()
{
    int a=0 ;
    char *nameofsibling=NULL;
    int ageofsibling=0;
    cout<<"Plz enter the number of Sibling";
    cin>>a;
    Sibling siblingobj[a];

    for(a=1;a<=a;a++)
    {
        cout<<"plz enter the particular of Sibling"<<a<<endl;
        cout<<"Name: ";
        cin>>nameofsibling;
        cout<<"Age: ";
        cin>>ageofsibling;
        siblingobj[a].setname(nameofsibling);
        siblingobj[a].setage(ageofsibling);
    }

    return 0;
    getch();
}

Dani AI

Generated

— the intention is clear but the posted code has several separate problems (memory-management and string handling, incorrect operator overload, out-of-bounds indexing, unreachable code after return, use of nonstandard headers). correctly flagged the operator-overloading misunderstanding; also noticed the unreachable getch() after return. Below is a small, modern and safe rewrite that produces the output you showed and avoids raw char* and manual new[]/delete[] mistakes.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits>

struct Sibling {
    std::string name;
    int age = 0;
};

int main() {
    std::cout << "Please enter the number of Sibling: ";
    int n;
    if (!(std::cin >> n) || n <= 0) return 1;

    std::vector<Sibling> v;
    v.reserve(n);
    for (int i = 1; i <= n; ++i) {
        Sibling s;
        std::cout << "Please enter the particulars of Sibling: " << i << '\n';
        std::cout << "Name: ";
        std::cin >> std::ws;
        std::getline(std::cin, s.name);
        std::cout << "Age: ";
        while (!(std::cin >> s.age) || s.age <= 0) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Enter a positive integer for age: ";
        }
        v.push_back(std::move(s));
    }

    auto youngest = std::min_element(v.begin(), v.end(),
        [](const Sibling &a, const Sibling &b){ return a.age < b.age; });
    auto eldest = std::max_element(v.begin(), v.end(),
        [](const Sibling &a, const Sibling &b){ return a.age < b.age; });

    if (youngest != v.end() && eldest != v.end()) {
        std::cout << "Youngest: " << youngest->name << "    Age: " << youngest->age << '\n';
        std::cout << "Eldest:   " << eldest->name << "    Age: " << eldest->age << '\n';
    }
    return 0;
}

Things fixed and tips:

  • Prefer std::string and std::vector to manage memory automatically; avoid conio.h and getch() (nonportable and unnecessary).
  • Do input validation (check cin and positive ages); start array/index loops at 0 or use i = 1..n consistently when prompting.
  • If you really need operator overloading, overload it with a user-defined operand (for example bool operator>(const Sibling&) const to compare ages) — as hinted. Otherwise use std::min_element/std::max_element or std::sort with a lambda.
  • Compile with g++ -std=c++11 -Wall -Wextra (or later).

Recommended Answers

All 2 Replies

In overloading you need atleast one user defined type

int main() {
    // ...

    return 0;
    getch();
}

Oops. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.