סדנא תרגיל 7 שאלה 1

benny++C, נכתב ע"י Benny, סדנא ב++C, תרגילים, תרגילים בסדנא ב++CLeave a Comment

תרגיל בפולימורפיזם (רב צורתיות) , מצביע מטיפוס האבא על טיפוס הבן, נותן לנו אפשרות לשייך פונקציות דומות בעלות אותו שם, כשכל פונקציה מתאימה לטיפוס ספציפי

תכנית ראשית

 /******************************
  
  targil: 7
  lecturer: david Cohen
  discription:  working whith polimorphizm (virtual functions) abstract class
  
  *******************************/



#include <iostream>
#include <string>
#include <vector>

using namespace std;
#include "Student.h"
#include "BA.h"
#include "MA.h"
#include "PHD.h"


// global function to print the students hwo Has the right to get scholarship
void print_milga(vector<Student*> studentim)
{

	for (int i = 0; i < 7; i++)
	{
		if (studentim[i]->milga())                           // if the studet deserve scholarship print the student info
		{                                                   // else go to the next studet
			studentim[i]->print(); 
		}

	}

}


int main()
{
	int arr1[3] = {90, 100, 90};                                         // prapae gradse info before insert to the student
	int arr2[10] = {100, 100, 90, 100, 90, 100, 90, 100, 90, 100};       // " "
	int arr3[7] = {90, 100, 90, 100, 90, 100, 90};                       // " " 
	
	
	vector<Student*> studentim;                                         //create vector for (few Kinds of) students
	studentim.push_back(new BA(123,"abe","avraham",3,arr1));            // insert the student info Through there constructors 
	studentim.push_back(new BA(234,"yitzhak","avrahamson",10,arr2));    // " " 
	studentim.push_back(new MA(345 ,"yaacov","jacobson",7,arr3,0));    // " "
	studentim.push_back(new MA(456 ,"sara","emanu",7,arr3,1));         // " "
	studentim.push_back(new PHD(567 ,"rivka","imanu",1,30));           // " "
	studentim.push_back(new PHD(678 ,"jacobs","rachel",2,20));         // " "
	studentim.push_back(new PHD(789 ,"leah","jacobs",2,30));           // " "

	
	print_milga(studentim);

	for (int i = 0; i < 7; i++)
	{
		delete studentim[i];                                      // release rendom memory

	}

	//system ("pause");
	return 0;
}


/**************OUTPUT*******************

ID: 234
first name: yitzhak
last name: avrahamson
num courses: 10
grades: 100 100 90 100 90 100 90 100 90 100
ID: 456
first name: sara
last name: emanu
num courses: 7
grades: 90 100 90 100 90 100 90
research: yes
ID: 789
first name: leah
last name: jacobs
num courses: 2
hours: 30
Press any key to continue . . .


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

המחלקה Student

#pragma once
#include <string>

class Student      // abstract calss (can not create Student viriable)
{
	int id;
	char* f_name;
	char* L_name;
	int num_courses;
public:

	int get_id();
	char* get_f_name();
	char* get_L_name();
	int get_num_courses();

	Student(int,char*,char*,int);
	virtual void print()=0;          // virtual function
	virtual	bool milga ()=0;
	~Student(void);
};


מימוש המחלקה Student

#include "Student.h"
#include <string>
#include <iostream>
using namespace std;


Student::Student(int id,char* f_name,char* L_name,int num_courses)
{
	this->id=id;
	this->f_name=f_name;
	this->L_name=L_name;
	this->num_courses=num_courses;
}

/*=========set get ==============*/

int Student::get_id()
{
	return id;
}
char* Student::get_f_name()
{
	return f_name;
}
char* Student::get_L_name()
{
	return L_name;
}
int Student::get_num_courses()
{
	return num_courses;
}
Student::~Student(void)
{
}

המחלקה BA

#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"


class BA:public Student   // inherete fron Studet class
{
	int* grades;
	int grades_size;
public:

	void set_grades(int n);
	int get_grades(int index);
	
	double average();
	virtual void print();
	virtual	bool milga ();
	BA(int,char*,char*,int,int*);
	~BA(void);
};


מימוש המחלקה BA

#include "BA.h"
#include "Student.h"
#include <iostream>
#include <string>

using namespace std;

BA::BA(int id,char* f_name,char* L_name,int num_courses,int*arr):Student(id, f_name, L_name,num_courses)
{
	grades=new int[num_courses];
	for (int i = 0; i < num_courses; i++)
	{
		grades[i]=arr[i];
	}
	this->grades_size=num_courses;
}

void BA::print()
{
	cout<<"ID: "<<this->get_id()<<endl;
	cout<<"first name: "<<this->get_f_name()<<endl;
	cout<<"last name: "<<this->get_L_name()<<endl;
	cout<<"num courses: "<<this->get_num_courses()<<endl;
	cout<<"grades: ";
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		cout<<grades[i]<<" ";
	}
	cout<<endl;
}
/*=========set get ==============*/
void BA::set_grades(int n)
{
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		grades[i]=n;
	}
}

int BA::get_grades(int index)
{
	return grades[index];
}


/*============================*/

double BA::average()
{
	int sum=0;
	double average;
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		sum+=grades[i];
	}
	average=sum/get_num_courses();
	return average;
}


// function that return true if the studet need to get milga

bool BA:: milga ()
{
	if (this->get_num_courses()>=10&&this->average()>95)
	{
		return true;
	}
	return false;
}

BA::~BA(void)
{
    delete[]grades;
}

המחלקה MA

#pragma once
#include <iostream>
#include <string>
#include "Student.h"
#include "BA.h"
using namespace std;
class MA:public BA   // inherete fron BA class
{
	bool research;
public:
	virtual void print();
	virtual	bool milga ();

	MA(int,char*,char*,int,int*,bool research);
	~MA(void);
};


מימוש המחלקה MA

#include "MA.h"
#include "BA.h"
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;

MA::MA(int id,char* f_name,char* L_name,int num_courses,int*arr,bool research):BA(id, f_name, L_name,num_courses,arr)
{
	this->research=research;
	
}

void MA::print()
{
	cout<<"ID: "<<this->get_id()<<endl;
	cout<<"first name: "<<this->get_f_name()<<endl;
	cout<<"last name: "<<this->get_L_name()<<endl;
	cout<<"num courses: "<<this->get_num_courses()<<endl;
	cout<<"grades: ";
	for (int i = 0; i < this->get_num_courses(); i++)
	{
		cout<<get_grades(i)<<" ";
	}
	cout<<endl;
	cout<<"research: ";
	if (this->research)   // if the student do a research 
	{
		cout<<"yes"<<endl;
	}
	else                    // if the student do not do a research 
	{
		cout<<"no"<<endl;
	}
}
// function that return true if the studet need to get milga
bool MA:: milga ()
{
	if (this->get_num_courses()>=7&&this->average()>90&&(this->research))
	{
		return true;
	}
		return false;
}


MA::~MA(void)
{
}

המחלקה PHD

#pragma once
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"
class PHD:public Student  // inherete fron Studet class
{
	int week_hours;
public:
	virtual	bool milga ();
	virtual void print();
	PHD(int,char*,char*,int,int);
	~PHD(void);
};


מימוש המחלקה PHD

#include "PHD.h"
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"

PHD::PHD(int id,char* f_name,char* L_name,int num_courses,int week_hours):Student(id, f_name, L_name,num_courses)
{
	this->week_hours=week_hours;
	
}


void PHD::print()
{
	cout<<"ID: "<<this->get_id()<<endl;
	cout<<"first name: "<<this->get_f_name()<<endl;
	cout<<"last name: "<<this->get_L_name()<<endl;
	cout<<"num courses: "<<this->get_num_courses()<<endl;
	cout<<"hours: "<<this->week_hours<<endl;
}

// function that return true if the studet need to get milga
 
bool PHD:: milga ()
{

	if (this->get_num_courses()==2&&this->week_hours>=25)
	{
		return true;
	}
		return false;
}


PHD::~PHD(void)
{
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *