473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

set new value for data member

tat
Dear C++ experts,

I have a piece of code (see below).
Alignment object has a data member that is a vector of Sequence
objects.
Sequence object, in turn, has a data member that is a vector of Residue
objects.
Residue object has a data member called m_Number.
After initializing, I want to update data member (m_Number of Residue
object) via method setNumber(const string& number). This setNumber
method is supposed to change m_Number in the Residue object. However,
it does not (There is nothing on the third column of output). Changing
"string m_Number" to "mutable string m_Number" did not help.

Can someone explain me why?

Thanks,
tat

----------------------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

class Residue {
public:
Residue(): m_Name(""), m_Number(""), m_Pos(0) {};
~Residue() {};

void initializeResidue(const char& name1, const int& align_pos) {
m_Name = name1;
m_Pos = align_pos;
}
void setName(const string& name)
{
m_Name = name;
}
string name() const
{
return m_Name;
}

void setNumber(const string& no)
{
m_Number = no;
}

string number() const {
return m_Number;
}

void setPos(const int& no)
{
m_Pos = no;
}

int pos() const {
return m_Pos;
}

private:
string m_Name;
string m_Number;
int m_Pos;
};

class Sequence {
string m_SeqName;
string m_Sequence;
vector<Residuem_Residues;
public:
Sequence(): m_SeqName(""), m_Sequence(""), m_Residues(0) {};
Sequence(const string& n, const string& s)
: m_SeqName(n), m_Sequence(s),m_Residues(0)
{
m_Residues.resize(m_Sequence.length());
for (int i = 0; i < int(m_Sequence.length()); i++) {
m_Residues.at(i).initializeResidue(m_Sequence.at(i ),i);
}
}

~Sequence() { m_Residues.clear();};

void setSequence(const string& seq) {
m_Sequence = seq;
m_Residues.resize(int(seq.length()));
for (int i = 0; i < int(seq.length()); i++) {
m_Residues.at(i).initializeResidue(seq.at(i),i);
}
}
string sequence() const {
return m_Sequence;
}
void setSeqName(const string& seq_name)
{
m_SeqName = seq_name;
}
string seqName() const {
return m_SeqName;
}

int length() const {
return m_Sequence.length();
}

Residue residue(const int& position) {
if (position >= m_Residues.size() || position < 0) {
cerr << "Index out of range\n";
exit(1);
}
else return m_Residues.at(position);
}
vector<Residueresidues() const {
return m_Residues;
}
void addResidue(Residue& res) {
m_Residues.push_back(res);
}
Sequence& operator=(const Sequence& xxx) {
if (this != &xxx) {
m_SeqName = xxx.seqName();
m_Sequence = xxx.sequence();
m_Residues = xxx.residues();
}
return *this;
}
};

typedef vector<SequenceVecS;

class Alignment {
Sequence m_RefSeq;
VecS m_ListOfSequences;
public:
Alignment(): m_ListOfSequences(0) {};
~Alignment() { m_ListOfSequences.clear(); };

Sequence refSeq() {
return m_RefSeq;
}
VecS listOfSeqs() const
{
return m_ListOfSequences;
}
void addSeq(Sequence& seq) {
m_ListOfSequences.push_back(seq);
}

void setRefSeq(const string& id) {
for (int i=0; i < int(m_ListOfSequences.size()); i++) {
if (m_ListOfSequences.at(i).seqName().find(id) != string::npos) {
m_RefSeq = m_ListOfSequences.at(i);
}
}
}

};

////////////////////////// MAIN ///////////////////////////////////

int main(int argc, char* argv[]) {
Alignment align;
Sequence seq1("1cje","KITVHFINRD");
Sequence seq2("Q5PPU4_XENLA","KLTVNFINRN");
Sequence seq3("Q9VZE1_DROME",".VNITFVRAN");
align.addSeq(seq1);align.addSeq(seq2);align.addSeq (seq3);
align.setRefSeq("1cje");
cout << align.refSeq().seqName() << "\t" << align.refSeq().sequence()
<< endl;

for (int i=0; i < align.refSeq().residues().size(); i++) {
cout << align.refSeq().residue(i).name() << "\t";
cout << align.refSeq().residue(i).pos() << "\t";
align.refSeq().residue(i).setNumber("pp");
cout << align.refSeq().residue(i).number() << "\n";
}
return 0;
}

Jul 27 '06 #1
2 1585
hi tat,
The method
> Residue residue(const int& position);
is returning the "Residue" by val. So changes to object returned by
this method will not get reflected in the object stored in vector of
Sequence class.

regards,
amir
tat wrote:
Dear C++ experts,

I have a piece of code (see below).
Alignment object has a data member that is a vector of Sequence
objects.
Sequence object, in turn, has a data member that is a vector of Residue
objects.
Residue object has a data member called m_Number.
After initializing, I want to update data member (m_Number of Residue
object) via method setNumber(const string& number). This setNumber
method is supposed to change m_Number in the Residue object. However,
it does not (There is nothing on the third column of output). Changing
"string m_Number" to "mutable string m_Number" did not help.

Can someone explain me why?

Thanks,
tat

----------------------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

class Residue {
public:
Residue(): m_Name(""), m_Number(""), m_Pos(0) {};
~Residue() {};

void initializeResidue(const char& name1, const int& align_pos) {
m_Name = name1;
m_Pos = align_pos;
}
void setName(const string& name)
{
m_Name = name;
}
string name() const
{
return m_Name;
}

void setNumber(const string& no)
{
m_Number = no;
}

string number() const {
return m_Number;
}

void setPos(const int& no)
{
m_Pos = no;
}

int pos() const {
return m_Pos;
}

private:
string m_Name;
string m_Number;
int m_Pos;
};

class Sequence {
string m_SeqName;
string m_Sequence;
vector<Residuem_Residues;
public:
Sequence(): m_SeqName(""), m_Sequence(""), m_Residues(0) {};
Sequence(const string& n, const string& s)
: m_SeqName(n), m_Sequence(s),m_Residues(0)
{
m_Residues.resize(m_Sequence.length());
for (int i = 0; i < int(m_Sequence.length()); i++) {
m_Residues.at(i).initializeResidue(m_Sequence.at(i ),i);
}
}

~Sequence() { m_Residues.clear();};

void setSequence(const string& seq) {
m_Sequence = seq;
m_Residues.resize(int(seq.length()));
for (int i = 0; i < int(seq.length()); i++) {
m_Residues.at(i).initializeResidue(seq.at(i),i);
}
}
string sequence() const {
return m_Sequence;
}
void setSeqName(const string& seq_name)
{
m_SeqName = seq_name;
}
string seqName() const {
return m_SeqName;
}

int length() const {
return m_Sequence.length();
}

Residue residue(const int& position) {
if (position >= m_Residues.size() || position < 0) {
cerr << "Index out of range\n";
exit(1);
}
else return m_Residues.at(position);
}
vector<Residueresidues() const {
return m_Residues;
}
void addResidue(Residue& res) {
m_Residues.push_back(res);
}
Sequence& operator=(const Sequence& xxx) {
if (this != &xxx) {
m_SeqName = xxx.seqName();
m_Sequence = xxx.sequence();
m_Residues = xxx.residues();
}
return *this;
}
};

typedef vector<SequenceVecS;

class Alignment {
Sequence m_RefSeq;
VecS m_ListOfSequences;
public:
Alignment(): m_ListOfSequences(0) {};
~Alignment() { m_ListOfSequences.clear(); };

Sequence refSeq() {
return m_RefSeq;
}
VecS listOfSeqs() const
{
return m_ListOfSequences;
}
void addSeq(Sequence& seq) {
m_ListOfSequences.push_back(seq);
}

void setRefSeq(const string& id) {
for (int i=0; i < int(m_ListOfSequences.size()); i++) {
if (m_ListOfSequences.at(i).seqName().find(id) != string::npos) {
m_RefSeq = m_ListOfSequences.at(i);
}
}
}

};

////////////////////////// MAIN ///////////////////////////////////

int main(int argc, char* argv[]) {
Alignment align;
Sequence seq1("1cje","KITVHFINRD");
Sequence seq2("Q5PPU4_XENLA","KLTVNFINRN");
Sequence seq3("Q9VZE1_DROME",".VNITFVRAN");
align.addSeq(seq1);align.addSeq(seq2);align.addSeq (seq3);
align.setRefSeq("1cje");
cout << align.refSeq().seqName() << "\t" << align.refSeq().sequence()
<< endl;

for (int i=0; i < align.refSeq().residues().size(); i++) {
cout << align.refSeq().residue(i).name() << "\t";
cout << align.refSeq().residue(i).pos() << "\t";
align.refSeq().residue(i).setNumber("pp");
cout << align.refSeq().residue(i).number() << "\n";
}
return 0;
}
Jul 28 '06 #2
tat
Hi Amir,

Thanks very much. I missed that. I actually make two mistakes. One is
what you pointed out. The other is the same kind of mistake but in the
Alignment class (Sequence refSeq()).
Now, it worked.

Regards,
tat

am******@yahoo.com wrote:
hi tat,
The method
Residue residue(const int& position);
is returning the "Residue" by val. So changes to object returned by
this method will not get reflected in the object stored in vector of
Sequence class.

regards,
amir
tat wrote:
Dear C++ experts,

I have a piece of code (see below).
Alignment object has a data member that is a vector of Sequence
objects.
Sequence object, in turn, has a data member that is a vector of Residue
objects.
Residue object has a data member called m_Number.
After initializing, I want to update data member (m_Number of Residue
object) via method setNumber(const string& number). This setNumber
method is supposed to change m_Number in the Residue object. However,
it does not (There is nothing on the third column of output). Changing
"string m_Number" to "mutable string m_Number" did not help.

Can someone explain me why?

Thanks,
tat

----------------------------------------------------------
#include<iostream>
#include<string>
#include<vector>
#include<fstream>

using namespace std;

class Residue {
public:
Residue(): m_Name(""), m_Number(""), m_Pos(0) {};
~Residue() {};

void initializeResidue(const char& name1, const int& align_pos) {
m_Name = name1;
m_Pos = align_pos;
}
void setName(const string& name)
{
m_Name = name;
}
string name() const
{
return m_Name;
}

void setNumber(const string& no)
{
m_Number = no;
}

string number() const {
return m_Number;
}

void setPos(const int& no)
{
m_Pos = no;
}

int pos() const {
return m_Pos;
}

private:
string m_Name;
string m_Number;
int m_Pos;
};

class Sequence {
string m_SeqName;
string m_Sequence;
vector<Residuem_Residues;
public:
Sequence(): m_SeqName(""), m_Sequence(""), m_Residues(0) {};
Sequence(const string& n, const string& s)
: m_SeqName(n), m_Sequence(s),m_Residues(0)
{
m_Residues.resize(m_Sequence.length());
for (int i = 0; i < int(m_Sequence.length()); i++) {
m_Residues.at(i).initializeResidue(m_Sequence.at(i ),i);
}
}

~Sequence() { m_Residues.clear();};

void setSequence(const string& seq) {
m_Sequence = seq;
m_Residues.resize(int(seq.length()));
for (int i = 0; i < int(seq.length()); i++) {
m_Residues.at(i).initializeResidue(seq.at(i),i);
}
}
string sequence() const {
return m_Sequence;
}
void setSeqName(const string& seq_name)
{
m_SeqName = seq_name;
}
string seqName() const {
return m_SeqName;
}

int length() const {
return m_Sequence.length();
}

Residue residue(const int& position) {
if (position >= m_Residues.size() || position < 0) {
cerr << "Index out of range\n";
exit(1);
}
else return m_Residues.at(position);
}
vector<Residueresidues() const {
return m_Residues;
}
void addResidue(Residue& res) {
m_Residues.push_back(res);
}
Sequence& operator=(const Sequence& xxx) {
if (this != &xxx) {
m_SeqName = xxx.seqName();
m_Sequence = xxx.sequence();
m_Residues = xxx.residues();
}
return *this;
}
};

typedef vector<SequenceVecS;

class Alignment {
Sequence m_RefSeq;
VecS m_ListOfSequences;
public:
Alignment(): m_ListOfSequences(0) {};
~Alignment() { m_ListOfSequences.clear(); };

Sequence refSeq() {
return m_RefSeq;
}
VecS listOfSeqs() const
{
return m_ListOfSequences;
}
void addSeq(Sequence& seq) {
m_ListOfSequences.push_back(seq);
}

void setRefSeq(const string& id) {
for (int i=0; i < int(m_ListOfSequences.size()); i++) {
if (m_ListOfSequences.at(i).seqName().find(id) != string::npos) {
m_RefSeq = m_ListOfSequences.at(i);
}
}
}

};

////////////////////////// MAIN ///////////////////////////////////

int main(int argc, char* argv[]) {
Alignment align;
Sequence seq1("1cje","KITVHFINRD");
Sequence seq2("Q5PPU4_XENLA","KLTVNFINRN");
Sequence seq3("Q9VZE1_DROME",".VNITFVRAN");
align.addSeq(seq1);align.addSeq(seq2);align.addSeq (seq3);
align.setRefSeq("1cje");
cout << align.refSeq().seqName() << "\t" << align.refSeq().sequence()
<< endl;

for (int i=0; i < align.refSeq().residues().size(); i++) {
cout << align.refSeq().residue(i).name() << "\t";
cout << align.refSeq().residue(i).pos() << "\t";
align.refSeq().residue(i).setNumber("pp");
cout << align.refSeq().residue(i).number() << "\n";
}
return 0;
}
Jul 28 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: ALI-R | last post by:
Hi All, First of all I think this is gonna be one of those threads :-) since I have bunch of questions which make this very controversial:-0) Ok,Let's see: I was reading an article that When...
40
by: Zach | last post by:
Can someone please explain what this means and illustrate the difference with some code. Thanks, Zach
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
6
by: dtschoepe | last post by:
Hi all, Working on homework again... I've got a weird problem, I've been banging my head against the wall on what is causing it. I have a pointer to a typdef named Person. At one point in the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
8
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
1
by: =?Utf-8?B?bWZt?= | last post by:
I have recently migrated to VS.Net 2005, and I am struggling to do something very basic with the combo box. I want to manually loop through a data table and add both the display text and the value...
1
by: jadeivel756 | last post by:
I BADLY NEED YOUR HELP...... HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C. My problem is that as I exit the...
4
by: jadeivel756 | last post by:
I BADLY NEED YOUR HELP...... HELP... hOW TO Pass value to a struct type and permanently store the data after youve given the data.The programming language is C. My problem is that as I exit the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.