Selles õpetuses õpime objekte objektile edastama ja objekti funktsioonilt C ++ programmeerimisel tagastama.
C ++ programmeerimisel võime objekte funktsioonile edastada sarnaselt tavaliste argumentide edastamisele.
Näide 1: C ++ edastab objektid funktsioonile
// C++ program to calculate the average marks of two students #include using namespace std; class Student ( public: double marks; // constructor to initialize marks Student(double m) ( marks = m; ) ); // function that has objects as parameters void calculateAverage(Student s1, Student s2) ( // calculate the average of marks of s1 and s2 double average = (s1.marks + s2.marks) / 2; cout << "Average Marks = " << average << endl; ) int main() ( Student student1(88.0), student2(56.0); // pass the objects as arguments calculateAverage(student1, student2); return 0; )
Väljund
Keskmine märk = 72
Siin oleme funktsiooni Student
argumentidena edastanud kaks objekti student1 ja student2 calculateAverage()
.

Näide 2: C ++ objekti tagastamine funktsioonist
#include using namespace std; class Student ( public: double marks1, marks2; ); // function that returns object of Student Student createStudent() ( Student student; // Initialize member variables of Student student.marks1 = 96.5; student.marks2 = 75.0; // print member variables of Student cout << "Marks 1 = " << student.marks1 << endl; cout << "Marks 2 = " << student.marks2 << endl; return student; ) int main() ( Student student1; // Call function student1 = createStudent(); return 0; )
Väljund
Märgid1 = 96,5 Märgid2 = 75

Selles programmis oleme loonud funktsiooni, createStudent()
mis tagastab Student
klassi objekti .
Oleme kutsunud createStudent()
alates main()
meetodit.
// Call function student1 = createStudent();
Siin hoiame createStudent()
meetodil tagastatud objekti õpilases1.