클래스를 이용한 간단한 프로그램 구현입니다.

shahn0718의 이미지

간단한 클래스를 이용하여 입력 한 후 더해서 보여주는 프로그래밍입니다.
다름이 아니라 출력을 하면 자꾸 1의 자리수만 등장하여 고민입니다.
변수 선언이라던지 주소 선언 같은 것이 잘못 되어 있으면 가르쳐주십시오
감사합니다.

#include <iostream>
using namespace std;
 
class AHN
{
 
public:
 
	int m_num1;
	int m_num2;
	int m_total;
 
 
	void Insert();
	void Sum();
 
};
 
void AHN::Insert()
{
	int  m_num1=0;
	int  m_num2=0 ;
	cout << "Insert NUM1 , NUM2: ";
	cin >> m_num1 >> m_num2;
}
 
void AHN::Sum()
{
 
	int m_total = m_num1 + m_num2;
 
}
 
 
int main()
{
 
	AHN a1;
	a1.Insert();
	a1.Sum();
	cout << "SUM =   " << & AHN::Sum;
	return 0;
 
}
shint의 이미지

책을 보시거나. 학원을 다니셔야 할거 같습니다.

OLC 센터

C 언어 첫 걸음
http://olc.oss.kr/lec/detail.jsp?lecid=389

C++
http://olc.oss.kr/lec/detail.jsp?lecid=186

#include <iostream>
using namespace std;
 
class AHN
{
 
public:
	AHN(){};
	~AHN(){};
	int m_num1;
	int m_num2;
	int m_total;
 
 
	void Insert();
	int Sum();
 
};
 
void AHN::Insert()
{
//	int  m_num1 = 0;
//	int  m_num2 = 0;
	cout < < "Insert NUM1 , NUM2: ";
	cin > > m_num1 > > m_num2;
}
 
int AHN::Sum()
{
 
	int m_total = m_num1 + m_num2;
	return m_total;
}
 
 
int main()
{
 
	AHN a1;
	a1.Insert();
	a1.Sum();
	cout < < "SUM =   " < < &AHN::Sum < < endl;
	printf("&AHN::Sum [ %x] &AHN::Sum [ %d]\n", &AHN::Sum, &AHN::Sum);
	printf("a1.Sum() [ %x]\n", a1.Sum());
	return 0;
 
}
 
// kldp 에서 소스코드를 올릴 때는
// #include <> 가 지워지는 것을 방지하기 위해서. <code> < /code> 태그를 사용해야 합니다.
// < < 와 > > 를 붙여서 쓰면. &lt;< >&gt; 로 보입니다.
// <code> 태그가 불편할 경우는 [ % 를 사용하셔도 됩니다. [ % 를 붙여서 쓰면. 문장 구분이 되서. 둘다. 한칸씩 띄워야 합니다. ㅇ_ㅇ;;
// XML <![CDATA[]]>로 사용하시면. 편하겠지만. 운영방식은 바뀌지 않네요. 추억에 장소가 될테니... 상관 없을지도 모릅니다. ㅇ_ㅇ;;
// 물론. 파일 첨부와 링크로 소스코드를 올리셔도 가능합니다.

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

twinwings의 이미지

좋은 코드는 아닌 것 같지만... 최소한으로 변경했습니다.

#include <iostream>
using namespace std;
 
class AHN
{
 
public:
 
	int m_num1;
	int m_num2;
	int m_total;
 
 
	void Insert();
	void Sum();
 
};
 
void AHN::Insert()
{
	//int  m_num1=0;
	//int  m_num2=0 ;
	//왜 동일한 이름으로 또 변수를 선언하시는지..?
	//멤버변수 이름이 가려집니다. 위에 선언된 변수는 지역변수입니다. 멤버변수가 아닙니다.
	//C++에는 이름을 찾는 방법인 쾨니그 탐색방법이란 것이 존재합니다. 이것에 의해 멤버변수 이름이 가려집니다.
	m_num1=0;
	m_num2=0 ;
	cout < < "Insert NUM1 , NUM2: ";
	cin > > m_num1 > > m_num2;
}
 
void AHN::Sum()
{
 
	//int m_total = m_num1 + m_num2;
	//왜 동일한 이름으로 또 변수를 선언하시는지..?
	//멤버변수 이름이 가려집니다. 위에 선언된 변수는 지역변수입니다. 멤버변수가 아닙니다.
	//C++에는 이름을 찾는 방법인 쾨니그 탐색방법이란 것이 존재합니다. 이것에 의해 멤버변수 이름이 가려집니다.
	m_total = m_num1 + m_num2;
 
}
 
 
int main()
{
 
	AHN a1;
	a1.Insert();
	a1.Sum();
	// cout < < "SUM =   " < < & AHN::Sum;
	// 질문자는 지금,
	// 1) 함수포인터 개념
	// 2) 지역변수
	// 3) 클래스의 멤버변수와 클래스의 스테틱변수를 구분 못하고 있습니다.
	cout < < "SUM =   " < < a1.m_total < < endl;
	return 0;
 
}

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.