/**********************************************************************************
* Name: Student info in C++, -- address.h
* Author: Hualin Li
* Date: 10/02/2015
* Description: This is student info management code in C++, one of the Header file.
***********************************************************************************/
#ifndef ADDRESS_H
#define ADDRESS_H
#include <string>
class Address{
public:
Address(std::string street, std::string city, std::string state, std::string zip) : street(street), city(city),state(state),zip(zip)
{
aString=street+' '+city+' '+state+' '+zip;
}
std::string street,city,state,zip;
std::string aString;
//private:
};
#endif
/**********************************************************************************
* Name: Student info in C++ -- academic.h
* Author: Hualin Li
* Date: 10/02/2015
* Description: This is student info management code in C++, one of the Header file.
***********************************************************************************/
class AcademicInfo{
public:
AcademicInfo(int credit, float gpa) : credit(credit),gpa(gpa)
{
//HenTaoyanzhezhongchushihua,zhenshitaitaoyanletaishaojianle.
}
int credit;
float gpa;
//private:
};
/**********************************************************************************
* Name: Student info in C++ -- name.h
* Author: Hualin Li
* Date: 10/02/2015
* Description: This is student info management code in C++, one of the Header file.
***********************************************************************************/
ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
//#include "Address.h"
class Name {
friend std::ostream &operator <<(std::ostream &os, const Name &name) {
if(name.middle!="")
os << name.last << ", "<<name.middle<<" ," << name.first;
else
os<< name.last <<", "<<name.first;
return os;
}
public:
Name(std::string last, std::string middle, std::string first) : last(last), first(first),middle(middle) {}
//private:
std::string last, first, middle;
};
#endif
/**********************************************************************************
* Name: Student info in C++ -- student.h
* Author: Hualin Li
* Date: 10/02/2015
* Description: This is student info management code in C++, one of the Header file.
***********************************************************************************/
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
#include "name.h"
#include "Address.h"
#include "AcademicInfo.h"
class Person {
friend std::ostream &operator <<(std::ostream &os, const Person &person);
public:
Person(const Name &name, int age, const Address &address,const AcademicInfo &academic);
Address address;
AcademicInfo academic;
std::string adr;
Name name;
int age;
std::string getName()
{
return name.last+','+name.first;
}
float getGPA()
{
return sGpa;
}
int getCreditsCompleted()
{
return sCredit;
}
void addGrade(int c, char g)
{
float score=0;
switch(g)
{
case 'A': score=4.0;
case 'a': score=4.0;
break;
case 'B': score=3.0;
case 'b': score=3.0;
break;
case 'C': score=2.0;
case 'c': score=2.0;
break;
case 'D': score=1.0;
case 'd': score=1.0;
break;
case 'F': score=0.0;
case 'f': score=0.0;
break;
default: score=0.0;
break;
}
sGpa=(sCredit*sGpa+c*score)/(sCredit+c);
sCredit=sCredit+c;
}
private:
float sGpa;
int sCredit;
};
#endif
/**********************************************************************************
* Name: Student info in C++ -- student.cpp
* Author: Hualin Li
* Date: 10/02/2015
* Description: This is student info management code in C++, the implementation file.
***********************************************************************************/
#include <iostream>
#include "student.h"
#include <string>
using namespace std;
Person::Person(const Name &name, int age, const Address &address, const AcademicInfo &academic) : name(name), age(age),address(address),academic(academic)
{
adr=address.aString;
sCredit=academic.credit;
sGpa=academic.gpa;
addGrade(0,0);
}
ostream &operator <<(ostream &os, const Person &person) {
os << person.name << " " << person.age<<" "<< "living in "<<person.adr<<", earning credits:"<<person.sCredit<<", with GPA:"<<person.sGpa;
return os;
}
/************************************************
* Name: Student info management in JAVA
* Author: Hualin Li
* Date: 09/02/2015
* Description: This is student info code in JAVA.
*************************************************/
import java.math.*;
class Person {
Person(Name name, int age,Address address,AcademicInfo academic) {
this.name = name;
this.age = age;
this.address=address;
this.academic=academic;
}
public String toString() {return name + " " + age+" "+address+", "+academic;}
Address address;
Name name;
int age;
AcademicInfo academic;
//The new functions are below
public String getName()
{
return name.toString();
}
//To get credit
public int getC()
{
return academic.getCredit();
}
//To get GPA
public double getG()
{
return academic.getGpa();
}
public static void main(String [] args ){
Person p=new Person(new Name("Doe","","Jane"),21,new Address("Park Ave","New York","New YorK","10005"),new AcademicInfo(104,3.5));
Person p2=new Person(new Name("Kate","IHateM","James"),24,new Address("Bay Parkway","Brooklyn","New YorK","11223"),new AcademicInfo(108,3.9));
System.out.println(p);
System.out.println(p2);
System.out.println("\n*********To tet the getName() function***********");
System.out.println("The student Jane's full name is "+p.getName());
System.out.println("\n*********To test the getCredit() and getGPA() functions********");
System.out.println("The student "+p.getName()+" earns total gredits "+p.getC()+" with GPA as "+p.getG()+".");
p.academic.addGrade(3,'A');
p2.academic.addGrade(3,'A');
System.out.println("\n***After adding one 3 credits class with A to both****");
System.out.println(p);
System.out.println(p2);
}
}
class Name {
Name(String last,String middle,String first) {
this.last = last;
this.middle=middle;
this.first = first;
}
public String toString()
{
if(middle=="")
return last + ", " + first;
else
return last+","+middle+","+first;
}
String last, first,middle;
}
class Address{
Address(String street,String city,String state,String zip)
{
this.street=street;
this.city=city;
this.state=state;
this.zip=zip;
adr=this.street+' '+this.city+' '+this.state+' '+this.zip;
}
public String adr;
String street,city,state,zip;
public String toString()
{
return adr;
}
}
class AcademicInfo{
AcademicInfo(int credit,double d){
this.credit=credit;
this.gpa=d;
}
public String toString()
{
return "earning credit:"+credit+" with GPA:"+gpa+".";
}
public int credit;
public double gpa;
public int getCredit() {return credit;}
public double getGpa() {return gpa;}
//Function to finally change score
void addGrade(int c, char S)
{
double score=0.0;
switch(S)
{
case 'A': score=4.0;
case 'a': score=4.0;
break;
case 'B': score=3.0;
case 'b': score=3.0;
break;
case 'C': score=2.0;
case 'c': score=2.0;
break;
case 'D': score=1.0;
case 'd': score=1.0;
break;
case 'F': score=0.0;
case 'f': score=0.0;
break;
default:
score=0.0;
break;
}
gpa=(double)Math.round((credit*gpa+c*score)/(credit+c)*100d)/100d;
credit=credit+c;
}
}
/**************************************************
* Name: Discussion of above code
* Author: Hualin Li
* Date: 09/02/2015
* Description: This is the discussion of both codes.
***************************************************/
In the JAVA example, we can use the following code to access the Person object in the Student class:
Class Student{
.
.
.
Person person;//Person is the class already written
//To access person parameter from here
double getGPA()
{
return person.getG();//getG() is the function in calss Person
}
int getCredit()
{
return person.getC()://getC() is the function in class person
}
//This is the GPA and credit for Student class
public double GPA;
public int credit
}
Then in the main function, we can get GPA and credit from object student defined from Class Student
public static void main(String[] args){
Student student;
//Getter to get GPA and credit from student object
double getGPAInMain()
{
return student.getGPA();
}
int getCreditInMain()
{
return student.getCredit();
}
}
Also, JAVA can use another method to achieve it :
student.person.getG();//getG() is the function in class Person()
For C++, you can use the first method, but not the second method.
回到主页
回到目录