lab2_2
gorazd
c_cpp
9 months ago
4.6 kB
7
Indexable
OOP_labs_stari
#include <cstring>
#include <iostream>
using namespace std;
class spaceObject
{
private:
char name[100];
char type[20];
long distanceFromEarth;
public:
spaceObject() : distanceFromEarth(0)
{
name[0] = '\0';
type[0] = '\0';
}
spaceObject(const char *_name, const char *_type, long _d) : distanceFromEarth(_d)
{
strncpy(name,_name, sizeof(name)-1);
name[sizeof(name)-1] = '\0';
strncpy(type,_type, sizeof(type)-1);
type[sizeof(type)-1] = '\0';
//cout<<"Space object initialized successfully!"<<endl;
}
~spaceObject() = default;
long get_distance_from_earth() const
{
return distanceFromEarth;
}
void set_distance_from_earth(long distance_from_earth)
{
distanceFromEarth = distance_from_earth;
}
char *get_name(){
return name;
}
void set_name(const char *_name)
{
strcpy(name,_name);
}
char *get_type(){
return type;
}
void set_type(const char *_type)
{
strcpy(type,_type);
}
};
class alien
{
char name[50];
int age;
char home[100];
int favs;
spaceObject *favorites;
public:
//def constructor
alien() : age(0), favs(0), favorites(nullptr) {name[0] = '\0'; home[0] = '\0';}
//constructor w parameters
alien(const char *_name,const char *_home, int _age, int _favs) : age(_age), favs(_favs), favorites(nullptr)
{
strcpy(name,_name);
strcpy(home,_home);
if(favs>0)
favorites = new spaceObject[favs];
}
//destructor
~alien()
{
delete[] favorites;
}
//deep copy constructor
alien(const alien& other) : age(other.age), favs(other.favs), favorites(nullptr)
{
strcpy(name, other.name);
strcpy(home, other.home);
if(favs>0)
{
favorites = new spaceObject[favs];
for (int i = 0; i<favs; i++)
{
favorites[i] = other.favorites[i];
}
}
}
char *get_name(){return name;}
void set_name(char *_name){strcpy(name,_name);}
char *get_home(){return home;}
void set_home(char *_home){strcpy(home,_home);}
int get_age() const
{
return age;
}
void set_age(int age)
{
this->age = age;
}
int get_favs() const
{
return favs;
}
void set_favs(int favs)
{
this->favs = favs;
}
spaceObject* get_favorites() const
{
return favorites;
}
void set_favorites(spaceObject* favorites, int fav)
{
if(fav>0)
{
delete[] this->favorites;
this->favorites = new spaceObject[fav];
for(int i = 0; i<fav; i++)
{
this->favorites[i] = favorites[i];
}
}
}
};
void show_favorite(alien &a, spaceObject &s)
{
cout<<"Alien name: "<<a.get_name()<<endl;
cout<<"Alien age: "<<a.get_age()<<endl;
cout<<"Alien homePlanet: "<<a.get_home()<<endl;
cout<<"Favourite space object closest to earth: "<<endl;
cout<<"Name: "<<s.get_name()<<endl;
cout<<"Type: "<<s.get_type()<<endl;
cout<<"Distance: "<<s.get_distance_from_earth()<<" light years"<<endl;
}
int main()
{
int n;
cin>>n;
for(int i = 0; i<n; i++)
{
char alienName[50], alienHome[100];
int alienAge, alienFavs;
cin>>alienName>>alienAge>>alienHome>>alienFavs;
cin.ignore();
alien myAlien(alienName, alienHome, alienAge,alienFavs);
spaceObject *favorites = new spaceObject[alienFavs];
//loop to set favorites
for(int i = 0; i<alienFavs; i++){
char name[100], type[20];
long distance;
cin>>name>>type>>distance;
//cin.ignore();
favorites[i] = spaceObject(name,type,distance);
}
myAlien.set_favorites(favorites,alienFavs);
spaceObject fav = favorites[0];
int max = favorites[0].get_distance_from_earth();
for(int i = 1; i<alienFavs; i++)
{
if(favorites[i].get_distance_from_earth()<max)
{
max = favorites[i].get_distance_from_earth();
fav = favorites[i];
}
}
delete[] favorites;
show_favorite(myAlien, fav);
}
return 0;
}Editor is loading...
Leave a Comment