Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Create a class children.

+12 votes
asked Jun 14, 2019 by mark (440 points)
Create a class children, it should contain child’s name, last name and age as private variables. Class also should have constructor which allows initialization of all private variables of the class. Create 10 element array of objects from class children, where all objects should be initialized using the constructor. Create 10 element array of void objects from class children (use override), sort the initial array of objects according to the children age and write the sorted objects in the second, void array.

2 Answers

+1 vote
answered Jan 21 by murali (200 points)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

class Children {
private:
    string name;
    string lastName;
    int age;

public:
    // Constructor
    Children(string n, string ln, int a) {
        name = n;
        lastName = ln;
        age = a;
    }

    // Getter for age (needed for sorting)
    int getAge() const {
        return age;
    }

    // Display function
    void display() const {
        cout << name << " " << lastName << " - Age: " << age << endl;
    }

    // Operator override for sorting
    bool operator<(const Children& other) const {
        return age < other.age;
    }
};
0 votes
answered Jan 21 by mandira (140 points)
/******************************************************************************

Welcome to GDB Online.

GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,

C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.

Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/

import java.util.*;

public class Main

{

public static void main(String[] args) {

    //children children=new children();

    List<children> ch=new ArrayList<children>();

    ch.add(new children("x","y",3));

     ch.add(new children("a","b",13));

      ch.add(new children("c","y",7));

       ch.add(new children("b","d",2));

       ch.sort(Comparator.comparing(children::getAge));

       for(children c:ch){

System.out.println(c.getFName()+" "+c.getLName()+" "+c.getAge());

       }

}

public static class children

    {

    private String fname;

    private String lname;

    private int age;

    public children(String fname,String lname,int age){

        this.fname=fname;

        this.lname=lname;

        this.age=age;

    }

    public String getFName(){

        return fname;

    }

    public String getLName(){

        return lname;

    }

    public int getAge(){

        return age;

    }

    }

    

}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...