Why can't I access the private variable of a class after defining a friend function? - operator-overloading

I've written a simple employee management project. I am facing problem when I am trying to assign values into the private variables of a class though I defined the operator overloading as friend function.
Here is my code:
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
class Person
{
string pName;
char pSex;
int pAge;
string pMob;
string pAddress;
public:
Person(){}
Person(string &pn, char &ps, int &pa, string &pm, string &pad):
pName(pn),pAge(pa),pMob(pm),pAddress(pad),pSex(ps){}
string getName(){return pName;}
int getAge(){return pAge;}
string getMob(){return pMob;}
string getAddress(){return pAddress;}
char getSex(){return pSex;}
};
class Employee:public Person
{
string eID;
string eJDate;
string eRank;
double eSalary;
public:
Employee(){}
Employee(string &pn, char &ps, int &pa, string &pm, string &pad, string &eid, string &ejd, string &er, double &es):
Person(pn,ps,pa,pm,pad),eID(eid),eJDate(ejd),eRank(er),eSalary(es){}
string getID(){return eID;}
string getJDate(){return eJDate;}
string getRank(){return eRank;}
double getSalary(){return eSalary;}
friend ostream& operator<<(ostream& os, Employee& ob);
friend istream& operator>>(istream& inf, Employee& ob);
};
ostream& operator<<(ostream& os, Employee& ob)
{
os<<ob.getName()<<endl;
os<<ob.getSex()<<endl;
os<<ob.getAge()<<endl;
os<<ob.getMob()<<endl;
os<<ob.getAddress()<<endl;
os<<ob.getID()<<endl;
os<<ob.getJDate()<<endl;
os<<ob.getRank()<<endl;
os<<ob.getSalary()<<endl;
return os;
}
istream& operator>>(istream& inf, Employee& ob)
{
inf>>ob.pName;
inf>>ob.pSex;
inf>>ob.pAge;
inf>>ob.pMob;
inf>>ob.pAddress;
inf>>ob.eID;
inf>>ob.eJDate;
inf>>ob.eRank;
inf>>ob.eSalary;
return inf;
}
int main()
{
cout<<"\t\t\t\tEnter your choice\n";
cout<<"\t\t\t\t-----------------\n";
cout<<"1: Enter an employee data."<<endl;
cout<<"2: View all the employee data."<<endl;
cout<<"Enter choice: ";
int choice;
cin>>choice;
if(choice==1)
{
string name,mob,address,id,jdate,rank;
int age;
char sex;
double salary;
ofstream out;
out.open("DB.txt",ios_base::app);
cout<<"Enter Name : ";
cin>>name;
cout<<"Enter Sex (M/F): ";
cin>>sex;
cout<<"Enter Age : ";
cin>>age;
cout<<"Enter Mobile No : ";
cin>>mob;
cout<<"Enter Address : ";
getline(cin,address);
cout<<"Enter ID : ";
cin>>id;
cout<<"Enter Join Date (dd-mm-yyyy): ";
cin>>jdate;
cout<<"Enter Position : ";
cin>>rank;
cout<<"Enter Salary : ";
cin>>salary;
Employee ob(name, sex, age, mob, address, id, jdate, rank, salary);
out<<ob;
}
else if(choice==2)
{
ifstream inf("DB.txt");
vector<Employee>ve;
Employee ob;
while(inf>>ob)
{
ve.push_back(ob);
}
for(int i=0; i<ve.size(); i++)
{
cout<<"\nEmployee No - "<<i<<endl;
cout<<"Employee Name: "<<ve[i].getName()<<endl;
cout<<"Sex: "<<ve[i].getSex()<<endl;
cout<<"Age: "<<ve[i].getAge()<<endl;
cout<<"Mobile: "<<ve[i].getMob()<<endl;
cout<<"Address: "<<ve[i].getAddress()<<endl;
cout<<"ID: "<<ve[i].getID()<<endl;
cout<<"Joining Date: "<<ve[i].getJDate()<<endl;
cout<<"Rank: "<<ve[i].getRank()<<endl;
cout<<"Salary: "<<ve[i].getSalary()<<endl;
}
}
return 0;
}
It is giving the following erros-
7|error: ‘std::string Person::pName’ is private|
63|error: within this context|
8|error: ‘char Person::pSex’ is private|
64|error: within this context|
9|error: ‘int Person::pAge’ is private|
65|error: within this context|
10|error: ‘std::string Person::pMob’ is private|
66|error: within this context|
11|error: ‘std::string Person::pAddress’ is private|
67|error: within this context|
||=== Build failed: 10 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Here I can access and assign value to the private variables of Employee class, but can't get access to the private variables of Person class.
Friend function doesn't work here?
How do I code to solve it? How do I load data from file into an object of Employee class?

When you declare a member private in a class it's private for the class itself, even to its children.
So pName in your example is inaccessible from Employee's child class.
If you declare it protected, then the child class can access it (and modify it)
In order to change it in the child class while still keeping the member private, you need to provide accessor (get/set methods) in the parent class. 'setPName' accessor can be public, or protected if you want to limit modification to the Employee class.

Related

Segmentation fault in header file

I have defined a Parent.h file, having Parent class with following data members:-
#pragma once
#include <string>
using namespace std;
class Person
{
private:
string id;
string name;
string email;
int contact_number;
string address;
public:
Person();
~Person();
void set_id(string);
void set_name(string);
void set_email(string);
void set_contact_number(int);
void set_address(string);
string get_id();
string get_name();
string get_email();
int get_contact_number();
string get_address();
};
Person::Person()
{
this->id = "";
this->name = "";
this->email = "";
this->contact_number = 0;
this->address = "";
}
void Person::set_id(string id)
{
this->id =id;
}
I have defined the rest of the functions(setters and getters) likewise in Parent.h file.
After that I am making a child class in Student.h header file, this Student class will publicly inherit the Parent class
#pragma once
#include"Courses.h" //course is aggregating to a student
#include"AcademicRecord.h"
#include "Person.h"
#include "Department.h"
#include<string>
#include<vector>
using namespace std;
class Student:public Person,public AcademicRecord,public Department
{
private:
string institute_email;
public:
Courses A;//A contains all information about the courses a student has opt for.
Student();
~Student();
//Using polymorphism between methods
void ShowStudentAcademicRecord(); //shows the academic record of current student
void ShowStudentCoursesInfo(); //getting the information for all courses of student
//setter
void set_institute_email(string email);
void setDepartmentID(string);
//getter
string get_institute_email();
};
After that I have a Display.h file having vector of Students as its Data member and using a member function in Display, I am asking user to input data members like ID,Name,Address,... for individual students.
#include "Student.h"
using namespace std;
class Display
{
public:
vector<Student> students;
Display();//default constructor
//setter
void setStudentData();
};
void Display::setStudentData()
{
int size;
cout<<"Enter the number of students: ";
cin>>size;
Student *current; //current Student
for (int i = 0; i < size; i++)
{
current = &students[i];
cout<<"For student "<<i+1<<"\nEnter the following: \n";//For general details
cout<<"ID Name Email Contact_Number Address Institute_Email\n";
string ID,Name,EMail,Address,insti_email;
int contact_number;
cin>>ID>>Name>>EMail>>contact_number>>Address>>insti_email;
current->set_id(ID);
current->set_name(Name);
current->set_email(EMail);
current->set_contact_number(contact_number);
current->set_address(Address);
current->set_institute_email(insti_email);
}
And when I try inserting value insides students by executing main.cpp file.
I get segmentation fault at run-time inside Person class at setter function, where the first setter I have declared inside the class is void set_id(string).
#include "Display.h"
#include<vector>
#include<string>
int main()
{
Display display;
display.setStudentData();
}
Error:-
I tried:-
1.Rechecking the setter function for any error
2.Bring all the classes inside the main.cpp file,instead of header files, but still the error continues.
I expected the program to take the desired input for various data members of student elments of vector students inside Display class.
Instead I got a seg-fault.
Please if anyone can tell me what I have done wrong, or do I have to do malloc somewhere, inside a function?
As Nate Eldredge, has said I should use push_back instead of accessing ith element of students vector in setStudentData() function, as initially during default declaration of a Display class, the students vector is empty. Hence trying to access students[i] in current = &students[i], I am getting segmentation fault.
The appropriate code for Display.h should be then
#include "Student.h"
using namespace std;
class Display
{
public:
vector<Student> students;
Display();//default constructor
//setter
void setStudentData();
};
void Display::setStudentData()
{
int size;
cout<<"Enter the number of students: ";
cin>>size;
total_students += size;
Student *temp =new Student(); //temp Student
for (int i = 0; i < size; i++)
{
cout<<"For student "<<i+1<<"\nEnter the following: \n";//For general details
cout<<"ID Name Email Contact_Number Address Faculty_Type Faculty_Description\n";
string ID,Name,EMail,Address,insti_email;
int contact_number;
cin>>ID>>Name>>EMail>>contact_number>>Address>>insti_email;
temp->set_id(ID);
temp->set_name(Name);
temp->set_email(EMail);
temp->set_contact_number(contact_number);
temp->set_address(Address);
temp->set_institute_email(insti_email);
cout<<"Enter the Course Details: \n";//For Course Details
int size_course;
cout<<"Enter the number of courses: ";
cin>>size_course;
cout<<"Enter the following for each course: \n";
cout<<"Course_Name Marks Attendance Percentage \n";
string name;
int marks;
float attendance;
for (int i = 0; i < size_course; i++)
{
cin>>name>>marks>>attendance;
temp->A.set_courses_enrolled(name);
temp->A.set_course_wise_marks(marks);
temp->A.set_coursewise_attendance_percentage(attendance);
}
cout<<"Enter the Academic Record Details: \n";//For Academic Record Details
string program_name; int admission_no, enroll_no, begin_year, end_year, credits; float CGPA;
cout<<"Enter Program_name, admission_no enroll_no begin_year end_year credits CGPA";
cin>>program_name>>admission_no>>enroll_no>>begin_year>>end_year>>credits>>CGPA;
setStudentAcademicRecord(temp,i,program_name,admission_no,enroll_no,begin_year,end_year,credits,CGPA); //passing temp pointer in this one
students.push_back(*temp); //adding a new student in students vector
}

Setting and getting a Global variable in Drools

I have something like this in drl file:
import java.lang.String
global String result;
rule ''Rule 1'' when some condition
then
result = "PASS";
kcontext.getKnowledgeRuntime().setGlobal("Result", result); // I got an "Unexpected global" exception.
System.out.println("result = "+ Result);
Also, I don't know how to access this global variable from my MyService.java class.
I was trying to set a global variable from the drl file not my java class like Service class.
All I had to do was the following and it worked successfully
import java.lang.String
global String result;
rule ''Rule 1''
when
some condition
then
String grade = "PASS";
kcontext.getKnowledgeRuntime().setGlobal("result", grade);
end
Also, the global variable name should match what I pass on the setGlobal("result",...).
And then get the global variable using the session I have in the Service class. like:
session.getGlobal("result");
Your rule should not be touching the 'kcontext'. What in the world are you trying to do? result = "PASS" is sufficient for setting the value of the global.
global String result
rule "Rule 1"
when
// some condition
then
result = "PASS";
end
Of course it's not going to work like you want it to because you need to change the value of the existing object; you can't overwrite it like that. Some options might be a "ResultsHolder" sort of class with a boolean variable you can set; or maybe even an AtomicBoolean that you can call set on.
To fire rules with a global, you need to add the global objects to the KieBase before invoking your rules:
var value = ...; // some OBJECT which you are going to pass in as a global
KieSession session = ruleBase.newStatefulSession();
session.insert(...); // insert data
session.setGlobal( "myGlobalFoo", value ); // sets the global; note the name must match the rule file!
session.fireAllRules();
After the rules are fired, you'll have your reference to value that you can use. This is also why you can't pass strings as globals and expect them to capture changes -- Java is pass-by-value, not pass-by-reference.
Here's an example for passing results out of the rules. This toy app will check the student's score on a test and then decide if they passed or failed.
Classes:
class Student {
private String name;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
}
class Exam {
private String name;
private Double score;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Double getScore() { return this.score; }
public void setScore(String score) { this.score = score; }
}
class ExamResults {
private List<String> results = new ArrayList<>();
public void logResults( String name, Double score, boolean passed ) {
this.results.add(name + " scored " + score + "%, which is a " + (passed ? "passing": "failing") + " grade.");
}
public List<String> getResults() { return this.results; }
}
Rule:
global ExamResults results;
rule "Evaluate exam"
when
Student( $name: name )
Exam ( $score: score, name == $name )
then
boolean passed = $score > 60.0;
results.logResults( $name, $score, passed );
end
Invocation:
List<Student> students = ...;
List<Exam> exams = ... ;
ExamResults results = new ExamResults();
KieSession session = ruleBase.newStatefulSession();
students.forEach( student -> session.insert(students) );
exams.forEach( exam -> session.insert(exam) );
session.setGlobal( "results", results);
session.fireAllRules();
// Print the results:
results.getResults().forEach(System.out::println);
If all you're trying to do is to get some data out of your rules (eg whether certain conditions match), I've written up answers about how to do that previously here and here. If you just want to know what rules triggered, you should write a listener which logs rule hits ("afterMatchFired").

How to return an object or struct from a method in MQL?

struct Person {
string FirstName;
string LastName;
};
class Builder {
public:
Person Builder::Build() {
Person person;
person.FirstName = "FirstName";
person.LastName = "LastName";
return person;
};
};
When I compile this it gives me the below error:
'return' - structure have objects and cannot be copied.
I just need to create a struct or class object and return it, I don't want to do any copying.
I tried using & and * combinations but didn't work. I tried with a class instead of struct and it didn't work either.
I also tried with class as shown below:
class Person {
public:
string FirstName;
string LastName;
};
class Builder {
public:
Person* Build() {
Person person;
person.FirstName = "FirstName";
person.LastName = "LastName";
return &person;
};
};
int OnInit()
{
Builder builder;
Person* person = builder.Build();
string firstName = person.FirstName;
return(INIT_SUCCEEDED);
}
And it gives me invalid pointer access when accessing person.FirstName in the OnInit() method at runtime.
Found the answer but how to avoid memory leak? how to destruct the object and its pointer after use?
class cPerson {
public:
string FirstName;
string LastName;
};
class cBuilder {
public:
cPerson* Build() {
cPerson* person = new cPerson();
person.FirstName = "firstname";
return person;
};
};
cBuilder builder;
cPerson* person = builder.Build();
string age = person.FirstName;
You can delete the object by delete(person); and if you are unsure the object is not a null, it is better to check if(CheckPointer(object)==POINTER_DYNAMIC)delete(object);
Overall you should have all such objects as variables with corresponding variable names, or keep them all in a collection and destroy the whole collection at end. You may also create global variable of object (before OnInit, not inside any function) and it is to be deleted at end of program.
Regarding the initial question - you cannot have string inside a struct, only primitives
you should NOT delete static objects - you will get "delete invalid pointer"-message from compiler in print area... therefore the check should be done as Daniel Kniaz answered... but as so as you are creating the object inside the wrapper - you'd better have a check for deletion & delete it in the Destructor of its wrapper (though I doubt, that you really should use here another class for CPerson creation - yuo can create its object in its - CPerson's - Constructor)
First you need a default constructor and copy constructor in your code. second you need to initialize the struct to default value, the struct only allocate space in the memory and not initialize it, so you can end up with cases where a long variable has some weird value like -1823834393939, so always set your struct to default values.
then when you return a class or struct from a function. the copy constructor will be called that copy the values. so if you don't want to return the exact object you've created in your class you don't need to return a reference
struct Person
{
string FirstName;
string LastName;
Person()
{
FirstName = "";
LastName = "";
}
Person(Person &that)
{
FirstName = that.FirstName;
LastName = that.LastName;
}
};
class Builder
{
public:
Person Builder::Build(string argFirstname, string argLastName)
{
Person person;
person.FirstName = argFirstname;
person.LastName = argLastName;
return person;
};
};
void OnStart()
{
Builder b;
Person p = b.Build("Mohammad Hossein", "amri");
Print(p.FirstName + " " + p.LastName);
}
the output will be

How to space first_name + last_name C++

My question is how can i use spaces between first_name and last_name after i instruct the user to input his name.
I am using code blocks and I'm kinda confused.
In Dev C++ i can do
cout<<first_name<<" "<<last_name;
Now what I am doing is just practicing C++ using variables in a class.
Here is my code.
#include<iostream>
#include<string>
using namespace std;
class DanielClass
{
public:
int setNameFunction(string first_name, string last_name)
{
cout<<"Enter your first name here: ";
cin>>first_name;
cout<<"Enter your last name here: ";
cin>>last_name;
name = first_name + last_name;
}
string getNameFunction()
{
return name;
}
private:
string name;
};
int main()
{
DanielClass NameObject;
NameObject.setNameFunction("", "");
cout<<NameObject.getNameFunction();
cout<<"\n\n";
return 0;
}
You can add string literals (which is just a term for a string entered directly into the code with quotes around it -- like the " " in the second line of code below) to string variables using the '+' operator.
You just need to change the line that reads:
name = first_name + last_name;
to:
name = first_name + " " + last_name;

Writing methods and constructors

OK, i need someone to explain to me where to start on this project.
First I need to overload the constructor by adding a default (no-args) constructor to Person that defines an object to have the name "N/A" and an id of -1.
Then i need to add a setter method named reset that can be used to reset the two private instance variables of this class to two values passed in as parameters.
Then I need to add a getter method named getName and getId that can be used to retrieve these two private variables
Here is the code:
public class Person
{
private String name;
private int id;
private static int personCount = 0;
// constructor
public Person(String pname)
{
name = pname;
personCount++;
id = 100 + personCount;
}
public String toString()
{
return "name: " + name + " id: " + id
+ " (Person count: " + personCount + ")";
}
// static/class method
public static int getCount()
{
return personCount;
}
////////////////////////////////////////////////
public class StaticTest
{
public static void main(String args[])
{
Person tom = new Person("Tom Jones");
System.out.println("Person.getCount(): " + Person.getCount());
System.out.println(tom);
System.out.println();
Person sue = new Person("Susan Top");
System.out.println("Person.getCount(): " + Person.getCount());
System.out.println(sue);
System.out.println("sue.getCount(): " + sue.getCount());
System.out.println();
Person fred = new Person("Fred Shoe");
System.out.println("Person.getCount(): " + Person.getCount());
System.out.println(fred);
System.out.println();
System.out.println("tom.getCount(): " + tom.getCount());
System.out.println("sue.getCount(): " + sue.getCount());
System.out.println("fred.getCount(): " + fred.getCount());
}
}
I'm not exactly sure where to start and I don't want just the answer. I'm looking for someone to explain this clearly.
First I need to overload the constructor by adding a default (no-args) constructor to Person that defines an object to have the name "N/A" and an id of -1.
Read about constructors here.
The Person class already contains a ctor that takes 1 argument. What you need to do is create a "default ctor" which is typically a ctor w/out any parameters.
Example:
class x
{
// ctor w/ parameter
//
x(int a)
{
// logic here
}
// default ctor (contains no parameter)
//
x()
{
// logic here
}
}
Then i need to add a setter method named reset that can be used to reset the two private instance variables of this class to two values passed in as parameters.
Setter methods are used to "encapsulate" member variables by "setting" their value via public function. See here.
Example:
class x
{
private int _number;
// Setter, used to set the value of '_number'
//
public void setNumber(int value)
{
_number = value;
}
}
Then I need to add a getter method named getName and getId that can be used to retrieve these two private variables
Getters do the opposite. Instead of "setting" the value of a private member variable, they are used to "get" the value from the member variable.
Example:
class x
{
private int _number;
// Getter, used to return the value of _number
//
public int getNumber()
{
return _number;
}
}
Hope this helps
I highly recommend consulting the Java Tutorials, which should be very helpful here. For example, there is a section on constructors which details how they work, even giving an example of a no-argument form:
Although Bicycle only has one constructor, it could have others, including a no-argument constructor:
public Bicycle() {
gear = 1;
cadence = 10;
speed = 0;
}
Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.
Similarly, there are sections dedicated to defining methods and passing information to them. There's even a section on returning values from your method.
Read the above and you should be able to complete your homework :-)